【问题标题】:need some tips on Drupal $form value需要一些关于 Drupal $form 值的提示
【发布时间】:2010-04-14 11:50:04
【问题描述】:

我得到了 dpm($form) 的工作。好的!这是查看数据的更好方法。我仍在试图弄清楚东西的来源,例如:位置经度和纬度。 “经度”这个词在 20 个不同的地方被引用。我认为这可能是隔离此输入字段的文本框的地方。 dpm($form['#field_info']['field_store_latitude']['location_settings']['form']['fields']);

关于如何追踪单个输入元素的任何提示?


**这不是答案,而是对我第一个问题的补充**

嗨 googletorp -

我正在尝试使用 hook_form_alter 修改现有表单。

经过几个小时的探索,我现在可以关闭表单的位置(经度/纬度)部分,如下所示:

未设置($form['field_store_latitude']);

但是,像这样关闭纬度是行不通的:
unset($form['field_store_latitude']['0']['#location_settings']['form']['fields']['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'] = '&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`

【问题讨论】:

  • 你能不能说的更准确点,你想做什么?找出哪些模块已将表单字段添加到表单(节点表单?)?

标签: php arrays drupal drupal-fapi


【解决方案1】:

所以有一种偷偷摸摸的方法来改变位置字段,你需要做的是使用#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 真的应该提高生产力吗?还是工作保障?
  • 位置模块是一个贡献模块,所以它实际上不是核心的一部分。它的构建方式非常适合可维护性,但很难覆盖它。
猜你喜欢
  • 1970-01-01
  • 2011-02-12
  • 1970-01-01
  • 1970-01-01
  • 2021-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-08
相关资源
最近更新 更多