一千萬個為什麽

搜索

需要一些關於Drupal $表單值的提示

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']);

有關如何追蹤單個輸入元素的任何提示?


**這不是答案,而是我第一個問題的補充**

嗨googletorp -

我正在嘗試使用hook_form_alter修改現有表單。

經過幾個小時的探索,我現在可以關閉這樣一個表單的位置(經度/緯度)部分:

未設置($形式[ 'field_store_latitude']);

但是,關閉這樣的緯度,不起作用:
未設置($形式[ 'field_store_latitude'] [ '0'] [ '#location_settings'] [ '形式'] [ '字段'] [ 'locpick']);

我找不到一種簡單的方法來鏈接html源代碼中的id和名稱與Krumo生成的數組。 在這種情況下,id稱為“edit-field-store-latitude-0-locpick-user-latitude”。

我需要一個配方或指南來識別Drupal形式的表單元素。


我想我已經找到了解決方案

<?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'] = ' ' . t('Use decimal notation.');
         $element['locpick']['user_longitude']['#description'] = ' ' . t('See our help page 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 '
';
            //print_r($form);
            dsm($form);
            print '
';     

           //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`

最佳答案

因此,有一種偷偷摸摸的方式來改變位置字段,你需要做的是使用 #after_built 回調:

/**
 * 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;
}

轉載註明原文: 需要一些關於Drupal $表單值的提示