如何在自定义标记中包装字段?

时间:2014-11-15 18:28:01

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

场合 我有一个自定义模块,它使用hook_field_formatter_info()来添加一个" fancy"内容类型的管理显示中的图像字段的选项。选择此选项后,我希望能够在自定义div和标记中包围ENTIRE字段。我希望解决方案是我的自定义模块中的一个钩子而不是模板覆盖。

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

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

这几天都在研究,我在这里淹死。

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

我发现它是:

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