hook_field_extra_fields:默认隐藏


Answers:


5

_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);

这对我没有用-不确定为什么对支持它的人同样有效。aklump的答案是唯一对我有用的解决方案。–
Felix Eve

3

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

也许我的问题还不够清楚。我确实想在表单上显示额外的字段(例如admin / structure / types / manage / article / display / full),但我想首先将formatter选择框设置为<hidden>
marcvangend 2011年

2
@marcvangend然后就不可能了hook_field_extra_fields()。您可以更改表单字段,也可以更改该字段使用的格式化程序。
kiamlaluno

好,谢谢。我不确定更改表单是否容易,因为我当然只想在未明确设置设置的情况下将格式化程序设置为隐藏...我将在有更多时间的情况下进行更多操作。
marcvangend 2011年


2

在模块的安装挂钩中,您可以调用与此函数类似的函数,默认情况下它将批量隐藏您的多余字段。

/**
 * 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);
  }
}

1
此代码有效。您可以按以下方式运行它:my_module_hide_extra_fields(array('facebook_like_button','contact_link'),'node',node_type_get_types());
batigolix 2014年

-1

如此简单的例子。对于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


我没有设法使此代码正常工作。$ view_modes没有正确声明(我在上面的代码段中对此进行了固定),并且未设置$ bundle变量
batigolix 2014年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.