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

发布于 2024-08-28 15:54:24 字数 2701 浏览 5 评论 0原文

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

关于如何追踪各个输入元素有什么建议吗?


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

hi googletorp -

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

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

unset($form['field_store_latitude']);

但是,像这样仅关闭纬度是行不通的:
取消设置($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`

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'] = ' ' . t('Use decimal notation.');
         $element['locpick']['user_longitude']['#description'] = ' ' . 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`

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

纸伞微斜 2024-09-04 15:54:24

所以有一种偷偷摸摸的方法可以改变位置字段,你需要做的是使用 #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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文