entity_agree-2.0.x-dev/src/Plugin/views/field/AgreementEntity.php

src/Plugin/views/field/AgreementEntity.php
<?php

namespace Drupal\entity_agree\Plugin\views\field;

use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Link to an agreement.
 *
 * @ViewsField("entity_agree_entity")
 */
class AgreementEntity extends FieldPluginBase implements ContainerFactoryPluginInterface {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    $field = new static($configuration, $plugin_id, $plugin_definition);
    $field->entityTypeManager = $container->get('entity_type.manager');
    return $field;
  }

  /**
   * {@inheritdoc}
   */
  public function query() {
    // No op. We're only rendering, not manipulating the query.
  }

  /**
   * {@inheritdoc}
   */
  public function clickSortable() {
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function usesGroupBy() {
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function render(ResultRow $values) {
    $entity = $this->getEntity($values);

    $agreement = $this->entityTypeManager->getStorage($entity->entity_type_id->value)
      ->load($entity->entity_id->value);

    if (!$agreement) {
      return NULL;
    }

    return [
      '#type' => 'link',
      '#title' => $agreement->label(),
      '#url' => $agreement->toUrl(),
    ];
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc