You are here

function paragraphs_field_formatter_view in Paragraphs 7

Implements hook_field_formatter_view().

File

./paragraphs.field_formatter.inc, line 75
Holds relevant functions for paragraph field formatters.

Code

function paragraphs_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  switch ($display['type']) {
    case 'paragraphs_view':

      // Prevent displaying useless markup if we don't have any values.
      if (empty($items)) {
        return $element;
      }

      // Get view mode from entity.
      $display_view_mode = empty($display['settings']['view_mode']) ? 'full' : $display['settings']['view_mode'];

      // Get view mode from field instance (if configured).
      $view_mode = empty($instance['display'][$display_view_mode]['settings']['view_mode']) ? $display_view_mode : $instance['display'][$display_view_mode]['settings']['view_mode'];
      $element['#theme_wrappers'] = array(
        'paragraphs_items',
      );
      $element['#attributes']['class'][] = drupal_clean_css_identifier('paragraphs-items');
      $element['#attributes']['class'][] = drupal_clean_css_identifier('paragraphs-items-view-mode-' . $view_mode);
      $element['#attributes']['class'][] = drupal_clean_css_identifier('paragraphs-items-field-' . $instance['field_name']);
      $element['#paragraphs_items_view_mode'] = $view_mode;
      foreach ($items as $delta => $item) {
        if ($paragraph = paragraphs_field_get_entity($item)) {
          $paragraph
            ->setHostEntity($entity_type, $entity, $langcode);
          if (entity_access('view', 'paragraphs_item', $paragraph)) {
            $element[$delta]['entity'] = $paragraph
              ->view($view_mode, $langcode);
          }
        }
      }
      break;
  }
  return $element;
}