2014-11-15 15 views
1

情况 我有一个使用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)); 
} 

一直在这几天,我淹没在这里。

回答

0

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

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

0

我发现了它是:

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