You are here

function paragraphs_jquery_ui_accordion_field_formatter_view in Paragraphs jQuery UI Accordion 7

Implements hook_field_formatter_view().

File

./paragraphs_jquery_ui_accordion.module, line 88
Paragraphs jQuery UI Accordion hooks and common functions.

Code

function paragraphs_jquery_ui_accordion_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  $settings = $display['settings'];
  $entity_ids = entity_extract_ids($entity_type, $entity);
  $entity_id = $entity_ids[0];
  $accordion_id = 'accordion-' . $entity_id;
  $element[0]['#attached'] = array(
    'library' => array(
      array(
        'system',
        'ui.accordion',
      ),
    ),
    'js' => array(
      array(
        'data' => drupal_get_path('module', 'paragraphs_jquery_ui_accordion') . '/js/paragraphs_jquery_ui_accordion.js',
        'type' => 'file',
      ),
      array(
        'data' => array(
          'paragraphs_jquery_ui_accordion' => array(
            'ids' => array(
              $entity_id => $accordion_id,
            ),
            'autoscroll' => $settings['autoscroll'],
          ),
        ),
        'type' => 'setting',
      ),
    ),
  );
  $element[0]['accordion'] = array(
    '#type' => 'container',
    '#attributes' => array(
      'id' => $accordion_id,
    ),
  );
  $paragraphs_ids = array();
  foreach ($items as $id => $item) {
    $paragraphs_ids[$id] = $item['value'];
  }
  $paragraphs_items = entity_load('paragraphs_item', $paragraphs_ids);
  foreach ($paragraphs_items as $paragraphs_item) {
    $display = array(
      'label' => 'hidden',
    );
    $paragraph_field = field_view_field('paragraphs_item', $paragraphs_item, $settings['content'], $display, $langcode = NULL);
    $render_paragraph_field = drupal_render($paragraph_field);
    $title = field_get_items('paragraphs_item', $paragraphs_item, $settings['title']);
    $paragraphs_unique_id = drupal_html_id(strtolower($title[0]['value']));
    $element[0]['accordion'][] = array(
      'title' => array(
        '#markup' => '<h3><a id="' . $paragraphs_unique_id . '" href="#' . $paragraphs_unique_id . '">' . $title[0]['value'] . '</a></h3>',
      ),
      'content' => array(
        '#markup' => '<div>' . $render_paragraph_field . '</div>',
      ),
    );
  }
  return $element;
}