需要一些关于Drupal $ form值的提示 [英] need some tips on Drupal $form value

查看:135
本文介绍了需要一些关于Drupal $ form值的提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有dpm($ form)工作。太好了!这是更好的方式来查看数据。我仍然在努力弄清楚哪些地方来自哪里,例如:位置经度&纬度。 经度一词在20个不同的地方引用。我认为这是一个可能的地方隔离输入字段的文本框。 DPM($形式[#field_info] [ field_store_latitude] [ location_settings’] [形式] [字段]);



有关如何追踪个人输入元素的任何提示?






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



hi googletorp -



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



经过几个小时的戳,我现在可以关闭一个窗体的位置(经度/纬度)部分,如这个:



unset($ form ['field_store_latitude']);



然而,只关闭纬度这样,不起作用:

unset($ form ['field_store_latitude'] ['0'] ['#location_settings'] ['form'] ['fields'] ['locpick']) ;



我找不到一个简单的方法来链接html源代码和名称与Krumo生成的数组。
在这种情况下,id被称为edit-field-store-latitude-0-locpick-user-latitude。



我需要一个食谱或指南以Drupal形式识别表单元素。






我想我确定了一个解决方案

 <?php 

//允许您更改位置字段,这是难以访问的。
//这将需要在这里描述的位置模块中的补丁:
// http://drupal.org/node/381458#comment-1287362

/ **
*实现自定义_element_alert()钩子。
* /

函数form_overrides_location_element_alter(& $ element){

//更改某些位置描述
$ element ['locpick'] [' user_latitude'] ['#description'] ='& nbsp;'。 t('使用十进制符号');
$ element ['locpick'] ['user_longitude'] ['#description'] ='& nbsp;'。 t('参见< a href =!url target = _blank>我们的帮助页面< / a>获取更多信息',array('!url'=> url('latlon_help')));

//或让它们完全消失
unset($ element ['locpick'] ['user_longitude']);
unset($ element ['locpick'] ['user_latitude']);
}


/ **
*执行form_alter钩子。
* /

函数form_overrides_form_alter(& $ form,$ form_state,$ form_id){
switch($ form_id){

case'user_profile_form ':
//更改用户个人资料表单中的标题
$ form ['account'] ['name'] ['#title'] = t('Login Name');
$ form ['account'] ['mail'] ['#title'] = t('Email');
break;

case'retailer_node_form':
//让我们检查应该是在这里...
print'< pre>';
// print_r($ form);
dsm($ form);
print'< / pre>';

//这可以删除城市
unset($ form ['field_myvar_latitude'] ['0'] ['#location_settings'] ['form'] ['fields'] ['市']);

//让我们试试#after_build属性
$ form ['#after_build'] [] ='mymodule_after_build_mynode';

break;
}
}

函数mymodule_after_build_mynode($ form,$ form_values){

//这不适用于位置字段

return $ form;
}`在这里输入代码


解决方案

有改变位置字段的鬼祟方式,您需要做的是使用 #after_built 回调:

  / ** 
*实现hook_form_alter()。
* /
函数mymodule_form_alter(& $ form,$ form_state,$ form_id){
if($ form_id =='x_node_form'){
//更改位置字段
if(isset($ form ['locations'])){
$ form ['locations'] ['#after_build'] [] ='mymodule_alter_location_field';
}
}
}

/ **
*从位置元素中删除删除复选框。
* /
函数mymodule_alter_location_field($ form_element,& $ form_state){
$ location = $ form_element [0]; //你可以改变的位置字段
return $ form_element;
}


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`

解决方案

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

这篇关于需要一些关于Drupal $ form值的提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆