I'm trying to create a new form element representing the bbcode editor, which is a compound object of the toolbar and native textarea 开发者_如何学Celement. So my hook_element_info() looks like:
function bbeditor_element_info() {
$type['bbeditor'] = array(
'#input' => TRUE,
'#cols' => 60,
'#rows' => 5,
'#resizable' => TRUE,
'#process' => array('process_bbeditor'),
'#theme_wrappers' => array('bbeditor', 'form_element'),
);
return $type;
}
But how do I get the name of element in the process function to passthrough it into the nested textarea element?
function process_bbeditor($element, &$form_state) {
...
// Insert the textarea element as a child.
$name = 'textarea'; // <------------- How do I get the name?
$element[$name] = $textarea;
return $element;
}
$form_state variable store the information of form's state (means you can use this variable to get the value of form element)
Like :
$form_state['element_name']['value'];
This will give you the value for form element. i'm not very sure that $form_state['element_name'] will give you the name of the element or it may return array.
Please check by using
var_dump($form_state['element_name']);
in your hook what it print
function process_bbeditor($element, &$form_state) {
...
var_dump($form_state['element_name']);
}
Ok, being aware that this may be a bit late for the OP, I just want to add an answer for the benefit of those that may stumble upon this :)
Assuming you are on a drupal 7 installation:
$element['#name']
contains the name of the element, provided it has a name. It will automatically receive a name if rendered via drupal_get_form, in which case it receives the name of the corresponding array element in the initial form array. It is also possible to set the #name
attribute directly, e.g.
$form['bbeditor_test'] = array(
'#type' => 'bbeditor',
'#name' => 'use-this-name',
// some more stuff ....
);
It often makes sense to re-use a _process
callback for custom fields defined for Drupal's field API (i.e. when defining fields via hook_field_info and related hooks). In such scenarios note, that the name of the element is not contained in the #name
attribute but in the #field_name
attribute.
So, assuming process_bbeditor
is also used for the field API fields we would have something like:
function process_bbeditor($element, &$form_state,$form) {
// some stuff ...
$element_name = '';
if (isset($element['#name'])) {
$element_name = $element['#name'];
}
elseif (isset($element['#field_name'])) {
$element_name = $element['#field_name'];
}
else { // to handle the rare case when drupal_render is called directly on the parent array and #name isn't set
$element_name = 'undefined';
}
// some more stuff using $element_name ....
return $element;
}
精彩评论