need some tips on Drupal $form value

2019-08-28 08:14发布

问题:

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:

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