【问题标题】:How to wrap field in custom markup?如何在自定义标记中包装字段?
【发布时间】:2014-11-15 18:28:01
【问题描述】:

情况 我有一个自定义模块,它使用 hook_field_formatter_info() 在内容类型的管理显示中向图像字段添加“花式”选项。选择此选项后,我希望能够在自定义 div 和标记中包围整个字段。我希望解决方案成为我的自定义模块中的一个钩子,而不是模板覆盖。

流程 用户希望图像字段的显示成为“花哨”选项。他们在下拉列表中检查它,然后从内容类型管理显示管理页面中单击保存。现在,在该内容类型节点上,即使有多个图像,整个字段也应该有自定义标记,新标记应该围绕所有图像,而不是每个单独的图像。

hook_field_formatter_view hook_field_formatter_view 似乎要求它的输出是一个数组,我似乎无法将输出包装在一个 div 中。

模板覆盖 我不能只为特定字段创建一个 field.tpl.php,因为就像我最初提到的那样,我需要能够切换主题并且模块仍然可以正常工作。除非有办法让我的模块覆盖该字段已专门设置 hook_field_formatter_info() 的任何字段。

/**
 * Implements hook_field_formatter_view().
 */
function bootstrap_modal_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();

  foreach ($items as $delta => $item) {
    if ($index === NULL || $index === $delta) {
      $element[$delta] = array(
        '#theme' => 'bootstrap_modal_image_formatter',
        '#item' => $item,
        '#entity_type' => $entity_type,
        '#entity' => $entity,
        '#node' => $entity, // Left for legacy support.
        '#field' => $field,
        '#display_settings' => $display['settings'],
        '#delta' => $delta,
      );
    }
  }


  // $element = '<div id="CUSTOMDIVSTUFF">' . $element . '</div>';

  return $element;
}

这里是#theme函数:

function theme_bootstrap_modal_image_formatter($variables) {

  $item = $variables['item'];
  $entity_type = $variables['entity_type'];
  $entity = $variables['entity'];
  $field = $variables['field'];
  $settings = $variables['display_settings'];

  $image = array(
    'path' => $item['uri'],
    'alt' => isset($item['alt']) ? $item['alt'] : '',
    'title' => isset($item['title']) ? $item['title'] : '',
    'style_name' => $settings['bootstrap_modal_node_style'],
  );

  if (isset($item['width']) && isset($item['height'])) {
    $image['width'] = $item['width'];
    $image['height'] = $item['height'];
  }

  if (isset($item['attributes'])) {
    $image['attributes'] = $item['attributes'];
  }

  // Allow image attributes to be overridden.
  if (isset($variables['item']['override']['attributes'])) {
    foreach (array('width', 'height', 'alt', 'title') as $key) {
      if (isset($variables['item']['override']['attributes'][$key])) {
        $image[$key] = $variables['item']['override']['attributes'][$key];
        unset($variables['item']['override']['attributes'][$key]);
      }
    }
    if (isset($image['attributes'])) {
      $image['attributes'] = $variables['item']['override']['attributes'] + $image['attributes'];
    }
    else {
      $image['attributes'] = $variables['item']['override']['attributes'];
    }
  }

  $entity_title = entity_label($entity_type, $entity);

  if ($style_name = $settings['bootstrap_modal_image_style']) {
    $path = image_style_url($style_name, $image['path']);
  }
  else {
    $path = file_create_url($image['path']);
  }

  $caption = 'some value';
  $gallery_id = 'some value';

  return theme('bootstrap_modal_imagefield', array('image' => $image, 'path' => $path, 'title' => $caption, 'gid' => $gallery_id));
}

已经为此工作了好几天,我在这里淹死了。

【问题讨论】:

    标签: drupal drupal-7 drupal-modules drupal-theming


    【解决方案1】:

    这可以通过HOOK_process_field() 实现。使用此挂钩通过将其添加到 theme_hook_suggestions 数组来为您的字段定义一个新的模板文件(将模板文件放在您的模块目录中,因为您想更改主题 - 正如您所提到的那样)。 More info about field template suggestions.

    然后,您需要将模块路径添加到 drupal 主题注册表,以便它选择模板文件。 Demo.

    【讨论】:

      【解决方案2】:

      我发现它是:

      function bootstrap_modal_field_formatter_view($entity_type, $entity, $field, $instance,       $langcode, $items, $display) {
        $element = array(
          '#prefix' => '<div class="wrapper">',
          '#suffix' => '</div>',
      );
      

      【讨论】:

        猜你喜欢
        • 2013-05-04
        • 1970-01-01
        • 1970-01-01
        • 2018-04-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-05
        相关资源
        最近更新 更多