You are here

EntityTypeInfo.php in Webform Analysis 8

File

src/EntityTypeInfo.php
View source
<?php

namespace Drupal\webform_analysis;

use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Manipulates entity type information.
 *
 * This class contains primarily bridged hooks for compile-time or
 * cache-clear-time hooks. Runtime hooks should be placed in EntityOperations.
 */
class EntityTypeInfo implements ContainerInjectionInterface {
  use StringTranslationTrait;

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $currentUser;

  /**
   * EntityTypeInfo constructor.
   *
   * @param \Drupal\Core\Session\AccountInterface $current_user
   *   Current user.
   */
  public function __construct(AccountInterface $current_user) {
    $this->currentUser = $current_user;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('current_user'));
  }

  /**
   * Adds devel links to appropriate entity types.
   *
   * This is an alter hook bridge.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface[] $entity_types
   *   The master entity type list to alter.
   *
   * @see hook_entity_type_alter()
   */
  public function entityTypeAlter(array &$entity_types) {
    $entity_types['webform']
      ->setFormClass('analysis', 'Drupal\\webform_analysis\\Form\\WebformAnalysisForm');
    $entity_types['webform']
      ->setLinkTemplate('results_analysis', '/admin/structure/webform/manage/{webform}/results/analysis');
  }

  /**
   * Adds devel operations on entity that supports it.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity on which to define an operation.
   *
   * @return array
   *   An array of operation definitions.
   *
   * @see hook_entity_operation()
   */
  public function entityOperation(EntityInterface $entity) {
    $operations = [];
    if ($this->currentUser
      ->hasPermission('view any webform submission')) {
      if ($entity
        ->hasLinkTemplate('results_analysis')) {
        $operations['analysis'] = [
          'title' => $this
            ->t('Analysis'),
          'weight' => 100,
          'url' => $entity
            ->toUrl('results_analysis'),
        ];
      }
    }
    return $operations;
  }

}

Classes

Namesort descending Description
EntityTypeInfo Manipulates entity type information.