在实现hook_field_extra_fields()时,是否可以默认隐藏额外的字段?
有时,提供额外的字段作为选项比较好,而不是在启用模块后立即显示它。
在实现hook_field_extra_fields()时,是否可以默认隐藏额外的字段?
有时,提供额外的字段作为选项比较好,而不是在启用模块后立即显示它。
Answers:
在_field_info_prepare_extra_fields()
默认情况下,多余的字段设置为可见,但是多余的字段设置存储在field_bundle_settings变量中,您可以在模块中进行更改,hook_install()
例如:
$entity_type = 'node';
$bundle = 'article';
$field_name = 'my_extra_field';
$view_mode = 'teaser';
$field_bundle_setting = variable_get('field_bundle_settings', array());
$field_bundle_setting[$entity_type][$bundle]['extra_fields']['display'][$field_name][$view_mode]['visible'] = FALSE;
variable_set('field_bundle_settings', $field_bundle_setting);
_field_extra_fields_pre_render()是field_attach_form()和field_attach_view()使用的渲染前回调,包含以下代码:
elseif (isset($elements['#view_mode'])) {
$view_mode = $elements['#view_mode'];
$extra_fields = field_extra_fields_get_display($entity_type, $bundle, $view_mode);
foreach ($extra_fields as $name => $settings) {
if (isset($elements[$name])) {
$elements[$name]['#weight'] = $settings['weight'];
// Visibility: make sure we do not accidentally show a hidden element.
$elements[$name]['#access'] = isset($elements[$name]['#access']) ? ($elements[$name]['#access'] && $settings['visible']) : $settings['visible'];
}
}
}
实现hook_field_extra_fields_display_alter(),您可以更改可见性设置,但是这会使字段在显示表单的所有时间中都不可见。如果不想在第一次加载表单时显示该字段,而在例如选择另一个表单字段或更改其值时显示该字段,则需要使用一些JavaScript代码。
function mymodule_field_extra_fields_display_alter(&$displays, $context) {
if ($context['entity_type'] == 'taxonomy_term' && $context['view_mode'] == 'full') {
$displays['description']['visible'] = FALSE;
}
}
<hidden>
。
hook_field_extra_fields()
。您可以更改表单字段,也可以更改该字段使用的格式化程序。
在模块的安装挂钩中,您可以调用与此函数类似的函数,默认情况下它将批量隐藏您的多余字段。
/**
* Sets default visibility of extra fields in all active view modes on entity bundles.
*
* This will not overwrite visibility that already exists in the variable.
*
* @param array $extra_field_names An array of extra fields
* @param string $entity_type The name of the entity type. Defaults to 'node'.
* @param array $bundles An array of bundle objects or names.
*/
function my_module_hide_extra_fields($extra_field_names, $entity_type = 'node', $bundles = NULL) {
$entity_info = entity_get_info($entity_type);
if ($bundles === NULL) {
$bundles = array_keys($entity_info['bundles']);
}
foreach ($bundles as $bundle) {
if (!is_string($bundle)) {
$bundle = $bundle->type;
}
$settings = field_bundle_settings($entity_type, $bundle);
$active_modes = array('default');
foreach ($settings['view_modes'] as $mode => $mode_settings) {
if ($mode_settings['custom_settings']) {
$active_modes[] = $mode;
}
}
foreach ($extra_field_names as $field_name) {
foreach ($active_modes as $mode) {
if (empty($settings['extra_fields']['display'][$field_name][$mode])
|| !array_key_exists('visible', $settings['extra_fields']['display'][$field_name][$mode])) {
$settings['extra_fields']['display'][$field_name][$mode]['visible'] = FALSE;
$settings['extra_fields']['display'][$field_name][$mode] += array(
'weight' => 0,
);
}
}
}
field_bundle_settings($entity_type, $bundle, $settings);
}
}
如此简单的例子。对于Drupal 7,在example_module目录下创建example_module.install
/**
* hook_install
*/
function example_module_install() {
$field_machine_name = 'new_extra_field';
$entity_types = array('node', 'taxonomy_term', 'user');
foreach ($entity_types as $type) {
$info = entity_get_info($type);
$settings = field_bundle_settings($type, $bundle);
$view_modes = array_merge(array('default'), array_keys($info['view modes']));
foreach($view_modes as $view_mode) {
$settings['extra_fields']['display'][$field_machine_name][$view_mode] = array(
'visible' => FALSE,
'weight' => 0,
);
}
field_bundle_settings($type, $bundle, $settings);
}
}
hook_install的文档 https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_install/7
在您的example_module.module文件中添加
/**
* Implementation of hook_field_extra_fields
*
*/
function example_module_field_extra_fields() {
$extra = array();
$field_machine_name = 'new_extra_field';
$entity_types = array('node', 'taxonomy_term', 'user');
foreach ($entity_types as $type) {
$info = entity_get_info($type);
foreach(array_keys($info["bundles"]) as $bundle) {
$extra[$type][$bundle]['display'][$field_machine_name] = array(
'label' => t('New extra field that will show up in manage display'),
'description' => 'A description for the extra field',
'weight' => 5,
);
}
}
return $extra;
}
hook_field_extra_fields的文档https://api.drupal.org/api/drupal/modules%21field%21field.api.php/function/hook_field_extra_fields/7