You are here

function paragraphs_table_field_formatter_view in Paragraphs table 7

Implements hook_field_formatter_view().

File

./paragraphs_table.module, line 132
Module file for Paragraph table module.

Code

function paragraphs_table_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  $settings = $display['settings'];
  $display_view_mode = empty($display['settings']['view_mode']) ? 'full' : $display['settings']['view_mode'];
  $view_mode = empty($instance['display'][$display_view_mode]['settings']['view_mode']) ? $display_view_mode : $instance['display'][$display_view_mode]['settings']['view_mode'];

  // Don't display anything for an empty collection.
  if (!empty($settings['hide_empty']) && empty($items)) {
    return $element;
  }
  switch ($display['type']) {
    case 'paragraphs_table_view':
      $header = [
        '',
        '',
      ];
      $field_names = array();
      $item = current($items);
      $paragraph = paragraphs_field_get_entity($item);
      $fields = field_info_instances('paragraphs_item', $paragraph
        ->bundle());
      $bundle_name = paragraphs_bundle_load($paragraph
        ->bundle())->name;
      foreach ($fields as $field_paragraphs_item) {
        $item_view_mode = !empty($field_paragraphs_item['display'][$view_mode]) ? $view_mode : 'default';
        if ($field_paragraphs_item['display'][$item_view_mode]['type'] != 'hidden') {
          $weight = $field_paragraphs_item['display'][$item_view_mode]['weight'];
          $field_names[$weight] = $field_paragraphs_item['field_name'];
          if ($field_paragraphs_item['display'][$item_view_mode]['label'] != 'hidden') {
            $header[$weight] = array(
              'data' => module_exists('i18n_field') ? i18n_field_translate_property($field_paragraphs_item, 'label') : $field_paragraphs_item['label'],
              'class' => $field_names[$weight],
            );
          }
          else {
            $header[$weight] = '';
          }
        }
      }
      if (module_exists('ds')) {

        // Add Display Suite fields and settings.
        $fields_ds = ds_get_fields('paragraphs_item');
        $fields_ds_settings = ds_get_field_settings('paragraphs_item', $field['field_name'], $view_mode);
        foreach ($fields_ds_settings as $name => $ds_settings) {
          if (isset($fields_ds[$name])) {
            $weight = $ds_settings['weight'];
            $field_names[$weight] = $name;
            $fields[$name]["label"] = $fields_ds[$name]['title'];
            $header[$weight] = array(
              'data' => $ds_settings['label'] != 'hidden' ? $fields_ds[$name]['title'] : NULL,
              'class' => $name,
            );
          }
          if (in_array($name, $field_names)) {
            $weight = array_search($name, $field_names);
          }

          // Allow to override field label using DS field format settings.
          if (isset($weight) && isset($ds_settings['formatter_settings']['ft']['lb'])) {
            $header[$weight]['data'] = t("@ds_settings", array(
              '@ds_settings' => check_plain($ds_settings['formatter_settings']['ft']['lb']),
            ));
          }
        }
      }
      if ($settings['vertical']) {
        $header = [
          $bundle_name,
          '',
        ];
      }
      ksort($header);
      ksort($field_names);
      $rows = array();
      $max_row = count($items);
      foreach ($items as $delta => $item) {
        $field_paragraphs = paragraphs_field_get_entity($item);
        if (empty($field_paragraphs)) {
          continue;
        }
        $view = $field_paragraphs
          ->view($view_mode);
        $id = $field_paragraphs
          ->identifier();
        $content = $view['paragraphs_item'][$id];
        $column = array();
        foreach ($field_names as $field_name) {
          if (isset($content[$field_name])) {
            $content[$field_name]['#label_display'] = 'hidden';
          }
          else {
            $content[$field_name] = array(
              '#markup' => '<span class="empty_field"></span>',
              '#empty' => TRUE,
            );
          }
          if (!$settings["vertical"]) {
            $column[] = array(
              'data' => $content[$field_name],
              'class' => drupal_html_class($field_name),
            );
          }
          else {
            if ($settings['hide_empty'] && !empty($content[$field_name]["#empty"])) {
              continue;
            }
            $fiels_value = [
              [
                'data' => $fields[$field_name]["label"],
              ],
              [
                'data' => $content[$field_name],
              ],
            ];
            $rows[] = array(
              'data' => $fiels_value,
              'class' => array(
                'field-paragraphs-item',
              ),
            );
          }
        }

        // Dont show option if in revision mode.
        $isrevision = arg(2) == 'revisions' ? TRUE : FALSE;

        // @todo add operation
        $isrevision = TRUE;
        $links = '';
        if (!$isrevision) {
          $links = array(
            '#theme' => 'links',
            '#theme_wrapper' => 'item_list',
            '#attributes' => array(
              'class' => array(
                'paragraphs-view-links',
              ),
            ),
          );
          $operation = [
            'edit',
            'delete',
          ];
          if (!empty($settings['duplicate']) && $settings['duplicate']) {
            $operation[] = 'duplicate';
          }
          foreach ($operation as $op) {
            if (!empty($settings[$op]) && entity_access($op == 'edit' ? 'update' : $op, 'paragraphs_item', $field_paragraphs)) {
              if (!empty($settings[$op])) {
                $setting_op = $settings[$op];
              }
              else {
                $setting_op = $op;
              }
              $links['#links'][$op] = array(
                'title' => t("@setting_op", array(
                  '@setting_op' => check_plain($setting_op),
                )),
                'href' => $field_paragraphs
                  ->path() . '/' . $op,
                'query' => drupal_get_destination(),
                'html' => TRUE,
                'attributes' => [
                  'title' => ucfirst(t('@op', array(
                    '@op' => $op,
                  ))),
                  'data-toggle' => "tooltip",
                ],
              );
              if ($op == 'duplicate') {
                $fied_path = 'paragraphs/' . strtr($content['#bundle'], array(
                  '_' => '-',
                ));
                $links['#links'][$op]['href'] = $fied_path . '/add/' . $settings['entity_type'] . '/' . $settings['entity_id'] . '/' . $field_paragraphs->item_id;
              }
              $header['operations'] = t('Operations');
            }
          }
        }
        if (!empty($links['#links'])) {
          $column[] = array(
            'data' => $links,
            'class' => "field-operations",
          );
        }
        if ($settings['vertical'] && $delta < $max_row - 1) {
          $break = array(
            'data' => [
              $bundle_name,
              $links,
            ],
            'class' => array(
              'paragraphs_table_bundle',
            ),
          );
          if (empty($links)) {
            $break['data'] = [
              [
                'data' => '<strong>' . $bundle_name . '</strong>',
                'colspan' => 2,
              ],
            ];
          }
          $rows[] = $break;
        }
        $rows[] = array(
          'data' => $column,
          'class' => array(
            'field-paragraphs-item',
          ),
        );
      }

      // Remove header if all labels are hidden.
      if (!array_filter($header)) {
        $header = array();
      }
      $element[0] = array(
        '#theme' => 'table',
        '#header' => $header,
        '#rows' => $rows,
        '#attributes' => array(
          'class' => array(
            'field-paragraphs-table-view',
            drupal_clean_css_identifier('view-mode-' . $view_mode),
          ),
        ),
      );
      if (!empty($settings['import'])) {
        $entityHost = $paragraph
          ->hostEntity();
        $variable_link_import = [
          $paragraph
            ->hostEntityType(),
          $paragraph->field_name,
          $paragraph
            ->hostEntityId(),
        ];
        $title = t($settings['import']);
        $attributes = [
          'class' => [
            'import',
          ],
        ];
        global $theme;
        $base_theme = drupal_find_base_themes(list_themes(), $theme);
        if (!empty($base_theme['bootstrap'])) {
          $title = "<i class='glyphicon glyphicon-import'></i> " . t($settings['import']);
          $attributes['class'][] = 'btn btn-warning';
        }
        $options = [
          'query' => drupal_get_destination(),
          'html' => TRUE,
          'attributes' => $attributes,
        ];
        $item_list = [
          '#theme' => 'item_list',
          '#list_type' => 'ul',
          '#items' => [
            l($title, 'quick_data/import/paragraphs_item/' . implode('/', $variable_link_import), $options),
          ],
          '#attributes' => [
            'class' => [
              'action-links',
            ],
          ],
        ];
        $element[0]["#suffix"] = drupal_render($item_list);
      }
      if (!empty($settings['empty'])) {
        _paragraphs_table_hide_empty($element[0]);
      }
      if (!empty($settings['datatables'])) {
        drupal_add_js('https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js', 'external');
        drupal_add_js('https://cdn.datatables.net/1.10.19/js/dataTables.bootstrap.min.js', 'external');
        drupal_add_js('https://cdn.datatables.net/buttons/1.5.2/js/dataTables.buttons.min.js', 'external');
        drupal_add_js('https://cdn.datatables.net/buttons/1.5.2/js/buttons.bootstrap.min.js', 'external');
        drupal_add_js('https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js', 'external');
        drupal_add_js('https://cdn.datatables.net/buttons/1.5.2/js/buttons.html5.min.js', 'external');
        drupal_add_css('https://cdn.datatables.net/1.10.19/css/dataTables.bootstrap.min.css', 'external');
        drupal_add_css('https://cdn.datatables.net/buttons/1.5.2/css/buttons.bootstrap.min.css', 'external');
        drupal_add_js(drupal_get_path('module', 'paragraphs_table') . '/js/datatables.js', 'file');
      }
      if (!empty($settings['ajax'])) {

        // @todo
      }
      break;
  }
  return $element;
}