You are here

function bibcite_entity_bibcite_reference_view_alter in Bibliography & Citation 8

Same name and namespace in other branches
  1. 2.0.x modules/bibcite_entity/bibcite_entity.module \bibcite_entity_bibcite_reference_view_alter()

Implements hook_bibcite_reference_view_alter().

File

modules/bibcite_entity/bibcite_entity.module, line 349
Module hooks.

Code

function bibcite_entity_bibcite_reference_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display) {
  if ($component = $display
    ->getComponent('bibcite_links')) {

    /** @var \Drupal\Component\Plugin\PluginManagerInterface $manager */
    $manager = \Drupal::service('plugin.manager.bibcite_link');
    $config = \Drupal::config('bibcite_entity.reference.settings');
    $build['bibcite_links'] = [
      '#type' => 'container',
      '#attributes' => [
        'class' => [
          'bibcite-links',
        ],
      ],
      '#weight' => $component['weight'],
      'links' => [
        '#theme' => 'item_list',
        '#attributes' => [
          'class' => [
            'inline',
          ],
        ],
        '#items' => [],
      ],
    ];
    $default_link_attributes = [
      'enabled' => TRUE,
      'weight' => 0,
    ];
    foreach ($manager
      ->getDefinitions() as $plugin_id => $definition) {
      $plugin_config = $config
        ->get("links.{$plugin_id}") ?: [];
      $plugin_config = $plugin_config + $default_link_attributes;
      if ($plugin_config['enabled']) {
        $instance = $manager
          ->createInstance($plugin_id);
        if ($link = $instance
          ->build($entity)) {
          $build['bibcite_links']['links']['#items'][] = $link + [
            '#weight' => $plugin_config['weight'],
          ];
        }
      }
    }
    uasort($build['bibcite_links']['links']['#items'], 'Drupal\\Component\\Utility\\SortArray::sortByWeightProperty');
  }
  if ($component = $display
    ->getComponent('bibcite_type')) {
    $bundle = ReferenceType::load($entity
      ->bundle());
    $build['bibcite_type'] = [
      '#type' => 'container',
      '#attributes' => [
        'class' => [
          'bibcite-type',
        ],
      ],
      '#weight' => $component['weight'],
      'type' => [
        '#markup' => Xss::filterAdmin($bundle ? $bundle
          ->label() : $entity
          ->bundle()),
      ],
    ];
  }
}