You are here

CustomContextualLinkListBuilder.php in Custom Contextual Links 8.2

Namespace

Drupal\ccl

File

src/CustomContextualLinkListBuilder.php
View source
<?php

namespace Drupal\ccl;

use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
use Drupal\Core\Entity\EntityInterface;
use Drupal\node\Entity\Node;

/**
 * Provides a listing of Custom Contextual Link entities.
 */
class CustomContextualLinkListBuilder extends ConfigEntityListBuilder {

  /**
   * {@inheritdoc}
   */
  public function buildHeader() {
    $header['label'] = $this
      ->t('Label');
    $header['title'] = $this
      ->t('Link Title');
    $header['url'] = $this
      ->t('URL');
    $header['type'] = $this
      ->t('Type');
    $header['options'] = $this
      ->t('Options');
    return $header + parent::buildHeader();
  }

  /**
   * {@inheritdoc}
   */
  public function buildRow(EntityInterface $entity) {

    /** @var \Drupal\ccl\Entity\CustomContextualLink $entity */
    $row['label'] = $entity
      ->label();
    $row['title'] = $entity
      ->get('title');
    $row['link'] = $entity
      ->get('link');
    $row['type'] = $entity
      ->get('type');
    $row['options'] = '';

    // Prepare the options display.
    if ($entity
      ->get('type') == 'node') {

      // Get content type names.
      $cts = node_type_get_names();
      switch ($entity
        ->getLinkOption('node_option')) {
        case 'global':
          $row['options'] = $this
            ->t('Attached to all nodes.');
          break;
        case 'ct':
          $row['options'] = $this
            ->t('Attached to all nodes of the content type %ct.', [
            '%ct' => $cts[$entity
              ->getLinkOption('node_type')],
          ]);
          break;
        case 'node':
          $nid = $entity
            ->getLinkOption('node_id');
          $node = $nid ? Node::load($nid) : NULL;
          if ($node) {
            $row['options'] = $this
              ->t('Attached to %node_title [NID: :nid].', [
              '%node_title' => $node
                ->label(),
              ':nid' => $node
                ->id(),
            ]);
          }
          break;
      }
    }
    else {
      foreach ($this
        ->moduleHandler()
        ->getImplementations('ccl_link_info') as $implementation) {
        $options = $this
          ->moduleHandler()
          ->invoke($implementation, 'ccl_link_info', [
          $entity,
        ]);
        if (!empty($options)) {
          $row['options'] = $options;
          break;
        }
      }
    }
    return $row + parent::buildRow($entity);
  }

}

Classes

Namesort descending Description
CustomContextualLinkListBuilder Provides a listing of Custom Contextual Link entities.