You are here

public function ContributorLabelFormatter::viewElements in Bibliography & Citation 8

Same name and namespace in other branches
  1. 2.0.x modules/bibcite_entity/src/Plugin/Field/FieldFormatter/ContributorLabelFormatter.php \Drupal\bibcite_entity\Plugin\Field\FieldFormatter\ContributorLabelFormatter::viewElements()

Builds a renderable array for a field value.

Parameters

\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.

string $langcode: The language that should be used to render the field.

Return value

array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.

Overrides EntityReferenceLabelFormatter::viewElements

File

modules/bibcite_entity/src/Plugin/Field/FieldFormatter/ContributorLabelFormatter.php, line 26

Class

ContributorLabelFormatter
Plugin implementation of the 'entity reference label' formatter.

Namespace

Drupal\bibcite_entity\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = parent::viewElements($items, $langcode);
  $show_role = $this
    ->getSetting('role');
  $show_category = $this
    ->getSetting('category');

  /* @var \Drupal\bibcite_entity\ContributorPropertiesServiceInterface $contributorPropertiesService */
  $contributorPropertiesService = \Drupal::service('bibcite_entity.contributor_properties_service');
  $roles = $contributorPropertiesService
    ->getRoles();
  $categories = $contributorPropertiesService
    ->getCategories();
  if ($show_role && !empty($roles) || $show_category && !empty($categories)) {
    $default_role_value = $contributorPropertiesService
      ->getDefaultRole();
    $default_role = isset($default_role_value) ? $roles[$default_role_value] : NULL;
    $default_category_value = $contributorPropertiesService
      ->getDefaultCategory();
    $default_category = isset($default_category_value) ? $categories[$default_category_value] : NULL;
    foreach ($items as $delta => $item) {
      $add = '';
      if ($show_role && !empty($roles)) {
        $role_value = $item
          ->get('role')
          ->getValue();
        if (isset($role_value, $roles[$role_value])) {
          $role = $roles[$role_value];
        }
        else {
          $role = $default_role;
        }
        if ($role) {
          $add .= ", {$role}";
        }
      }
      if ($show_category && !empty($categories)) {
        $category_value = $item
          ->get('category')
          ->getValue();
        if (isset($category_value, $categories[$category_value])) {
          $category = $categories[$category_value];
        }
        else {
          $category = $default_category;
        }
        if ($category) {
          $add .= ", {$category}";
        }
      }
      if (isset($elements[$delta]['#type']) && $elements[$delta]['#type'] == 'link') {
        $elements[$delta]['#title'] .= $add;
      }
      else {
        $elements[$delta]['#plain_text'] .= $add;
      }
    }
  }
  return $elements;
}