You are here

function colorbox_node_field_attach_view_alter in Colorbox Node 7.3

Implements hook_field_attach_view_alter().

File

./colorbox_node.module, line 536
Creates a menu callback with support for displaying a node inside of a colorbox.

Code

function colorbox_node_field_attach_view_alter(&$output, $context) {
  foreach (element_children($output) as $field_name) {
    $element =& $output[$field_name];
    $field = field_info_field($field_name);
    if (!isset($field['settings']['target_type'])) {
      continue;
    }

    // Load up the rest of our data since we have a target type.
    $instance = field_info_instance($element['#entity_type'], $field_name, $element['#bundle']);
    $entity = entity_get_info($field['settings']['target_type']);
    $display = $instance['display']['default'];
    $settings = $display['settings'];

    // In views, we will have a localized context for our settings.
    // Set them here to override the defaults.
    if (isset($context['display']) && isset($context['display']['settings'])) {
      $settings = $context['display']['settings'];
    }

    // If we are not linking as a colorbox, then just continue on.
    if (!isset($settings['colorbox_node_link']) || !$settings['colorbox_node_link']) {
      continue;
    }
    $handler = entityreference_get_selection_handler($field, $instance, $element['#entity_type'], $entity);
    foreach (element_children($element) as $delta) {
      $label = $handler
        ->getLabel($element['#items'][$delta]['entity']);
      if ($uri = entity_uri($field['settings']['target_type'], $element['#items'][$delta]['entity'])) {
        $class = array();
        if (!empty($settings['colorbox_node_classes'])) {
          $class = array_unique(explode(' ', trim($settings['colorbox_node_classes'])));
        }
        $class[] = 'colorbox-node';
        $uri['options']['attributes'] = array(
          'class' => $class,
          'data-inner-width' => $settings['colorbox_node_width'],
          'data-inner-height' => $settings['colorbox_node_height'],
        );
        $element[$delta]['#markup'] = l($label, $uri['path'], $uri['options']);
      }
    }
  }
}