You are here

function magnific_popup_field_formatter_view in Magnific Popup 7

Implements hook_field_formatter_view().

File

includes/magnific_popup.formatters.inc, line 241
Formatters for Magnific Popup.

Code

function magnific_popup_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  if (!_magnific_popup_check_status()) {
    return FALSE;
  }
  $element = array();

  // Create the main container.
  $settings = $display['settings'];
  $popup_gallery = array(
    '#type' => 'container',
    '#attributes' => array(
      'class' => array(),
    ),
  );

  // Determine if/what the allowed trigger item is (which will get a thumbnail).
  $trigger_item = key($items);
  if ($settings['gallery_style'] == 'random_item') {
    $trigger_item = array_rand($items);
  }

  // Figure out the base-class for the gallery/nogallery, which is generally
  // something like 'mfp-nogallery-image' or 'mfp-gallery-iframe'.
  $gallery_base_class = $settings['gallery_style'] == 'no_gallery' ? 'mfp-nogallery' : 'mfp-gallery';
  if ($field['type'] == 'image') {
    $gallery_base_class .= '-image';
  }
  else {
    $gallery_base_class .= '-iframe';
  }
  $popup_gallery['#attributes']['class'][] = $gallery_base_class;

  // Add each item to our container.
  foreach ($items as $delta => $item) {
    $popup_gallery['item-' . $delta] = array(
      '#theme' => 'link',
      '#text' => '',
      '#path' => $item['target_uri'],
      '#options' => array(
        'attributes' => array(
          'class' => $item['classes'],
          'style' => array(),
        ),
        'html' => TRUE,
      ),
    );
    if (!empty($item['oembed_html'])) {
      $popup_gallery['item-' . $delta]['#options']['attributes']['oembed'] = $item['oembed_html'];
    }
    $popup_gallery['item-' . $delta]['#options']['attributes']['class'][] = 'mfp-item';

    // This item has a thumbnail image if:
    // - the Gallery Type is "all items" (because all items have thumbs), or
    // - the Gallery Type is "no gallery", or
    // - the Gallery Type is "first item" and this $delta is the first item, or
    // - the Gallery Type is "random item" and this $delta is the random item.
    if ($settings['gallery_style'] == 'all_items' || $settings['gallery_style'] == 'no_gallery' || $delta == $trigger_item) {
      $image = array(
        // Try to use a "figure" theme implementation if it exists, or fall back
        // to "image".
        '#theme' => array(
          'figure',
          'image',
        ),
        '#path' => $item['thumbnail_path'],
        '#alt' => $item['alt'],
        '#title' => $item['title'],
        '#attributes' => array(
          'class' => array(
            'mfp-thumbnail',
          ),
        ),
      );
      $popup_gallery['item-' . $delta]['#text'] = render($image);
    }
    else {

      // Normally the title comes from img.mfp-thumbnail's title attribute.
      // Since this a.mfp-item will not have a thumbnail image, add the title to
      // a special span.mfp-title's title attribute.
      $popup_gallery['item-' . $delta]['#text'] = t('<span class="mfp-title" title="@title">&nbsp;</span>', array(
        '@title' => $item['title'],
      ));

      // Hide the a.mfp-item (and any span.mfp-title).
      $popup_gallery['item-' . $delta]['#options']['attributes']['style'][] = 'display: none;';
    }
  }
  if (count($items)) {
    $element[0] = $popup_gallery;
  }
  return $element;
}