nodeapi_example_form_alter
- Drupal 5
- Drupal 6
developer/examples/nodeapi_example.module, строка 22
- Версии
- 5
nodeapi_example_form_alter($form_id, &$form)
- 6
nodeapi_example_form_alter(&$form, $form_state, $form_id)
Реализация hook_form_alter()
.
С помощью этого хука можно изменить любую форму. Мы внесём изменения только в два типа: в конфигуратор типа ноды и в формы редактирования.
У администраторов должна быть возможность указать, у каких типов контента должно быть наше добавленное поле оценки. Это сделано вставкой переключателя на странице конфигурирования типов контента.
Код
<?php
function nodeapi_example_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'node_type_form' && isset($form['identity']['type'])) {
// Alter the node type's configuration form to add our setting. We don't
// need to worry about saving this value back to the variable, the form
// we're altering will do it for us.
$form['workflow']['nodeapi_example'] = array(
'#type' => 'radios',
'#title' => t('NodeAPI Example Rating'),
'#default_value' => variable_get('nodeapi_example_'. $form['#node_type']->type, 0),
'#options' => array(0 => t('Disabled'), 1 => t('Enabled')),
'#description' => t('Should this node have a rating attached to it?'),
);
}
// If the type and node field are set this may be a node edit form.
else if (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] .'_node_form' == $form_id) {
// If the rating is enabled for this node type, we insert our control
// into the form.
$node = $form['#node'];
if (variable_get('nodeapi_example_'. $form['type']['#value'], 0)) {
$form['nodeapi_example_rating'] = array(
'#type' => 'select',
'#title' => t('Rating'),
'#default_value' => isset($node->nodeapi_example_rating) ? $node->nodeapi_example_rating : '',
'#options' => array(0 => t('Unrated'), 1, 2, 3, 4, 5),
'#required' => TRUE,
'#weight' => 0,
);
}
}
}
?>
Войдите или зарегистрируйтесь, чтобы получить возможность отправлять комментарии