dpa31905 2010-04-14 11:50
浏览 30
已采纳

需要一些关于Drupal $ form值的提示

I got dpm($form) working. Nice! This is much better way to view data. I am still trying to figure out where stuff is coming from eg: location longitude & latitude. The word 'longitude' is referenced in 20 different places. I thought this was a likely place to isolate text box for this input field. dpm($form['#field_info']['field_store_latitude']['location_settings']['form']['fields']);

Any tips on how to track down individual input elements?


** this is not an answer, but a supplement to my first question **

hi googletorp -

I am trying to modify existing forms using hook_form_alter.

After several hours of poking around, I can now turn off location (longitude/latitude) section of a form like this:

unset($form['field_store_latitude']);

However, turning off just the latitude like this, does not work:
unset($form['field_store_latitude']['0']['#location_settings']['form']['fields']['locpick']);

I cannot find a easy way to link id and names in html source with arrays produced by Krumo. In this case id is called "edit-field-store-latitude-0-locpick-user-latitude".

I need a recipe or guidelines for identifying form elemets in Drupal form.


I think I nailed down a solution

<?php

    // allows you to alter locations fields, which are tricky to access.
    // this will require a patch in location module described here:
    // http://drupal.org/node/381458#comment-1287362

    /**
    * Implementation of custom _element_alert() hook.   
    */

    function form_overrides_location_element_alter(&$element){

        // change some location descriptions
         $element['locpick']['user_latitude']['#description'] = '&nbsp;' . t('Use decimal notation.');
         $element['locpick']['user_longitude']['#description'] = '&nbsp;' . t('See <a href=!url target=_blank>our help page</a> for more information.', array('!url' => url('latlon_help')));

        // or make them disappear entirely
        unset($element['locpick']['user_longitude']);
        unset($element['locpick']['user_latitude']);
    }


    /**
    * Implementation of form_alter hook.    
    */

    function form_overrides_form_alter(&$form, $form_state, $form_id) {
    switch ($form_id) {

        case 'user_profile_form':
            // change titles in user profile form
             $form['account']['name']['#title'] = t('Login Name');         
             $form['account']['mail']['#title'] = t('Email');          
        break;

        case 'retailer_node_form':      
        // let's check what is supposed to be here...
            print '<pre>';
            //print_r($form);
            dsm($form);
            print '</pre>';     

            // this works to remove the city
            unset($form['field_myvar_latitude']['0']['#location_settings']['form']['fields']['city']);

            // let's try #after_build property
            $form['#after_build'][]='mymodule_after_build_mynode';

        break;
    }
  }

function mymodule_after_build_mynode($form, $form_values) {

    // This will not work for locations fields

    return $form;
}`enter code here`
  • 写回答

1条回答 默认 最新

  • duanboshe0001 2010-04-15 12:30
    关注

    So there is sneaky way to alter the location field, what you need to do is to use the #after_built callback:

    /**
     * Implements hook_form_alter().                                     
     */
    function mymodule_form_alter(&$form, $form_state, $form_id) {
      if ($form_id == 'x_node_form') {
        // alter the location field
        if (isset($form['locations'])) {
          $form['locations']['#after_build'][] = 'mymodule_alter_location_field';
        }
      }
    }
    
    /**
     * Remove the delete checkbox from location element.
     */
    function mymodule_alter_location_field($form_element, &$form_state) {
      $location = $form_element[0]; // The location field which you can alter
      return $form_element;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能