You are here

FieldBlockDeriver.php in Field as Block 8.2

File

src/Plugin/Derivative/FieldBlockDeriver.php
View source
<?php

namespace Drupal\fieldblock\Plugin\Derivative;

use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\fieldblock\Controller\FieldBlockController;

/**
 * Provides block plugin definitions for fieldblock blocks.
 *
 * @see \Drupal\fieldblock\Plugin\Block\FieldBlock
 */
class FieldBlockDeriver extends DeriverBase implements ContainerDeriverInterface {
  use StringTranslationTrait;

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

  /**
   * Constructs a FieldBlockDeriver object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   The entity type manager.
   */
  public function __construct(EntityTypeManagerInterface $entityTypeManager) {
    $this->entityTypeManager = $entityTypeManager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, $base_plugin_id) {
    return new static($container
      ->get('entity_type.manager'));
  }

  /**
   * {@inheritdoc}
   */
  public function getDerivativeDefinitions($base_plugin_definition) {
    $fb_controller = new FieldBlockController();
    $definitions = $this->entityTypeManager
      ->getDefinitions();
    foreach ($definitions as $entity_type_id => $definition) {
      if ($fb_controller
        ->isBlockableEntityType($definition)) {
        $this->derivatives[$entity_type_id] = $base_plugin_definition;
        $this->derivatives[$entity_type_id]['admin_label'] = $this
          ->t('@type field', [
          '@type' => $definition
            ->getLabel(),
        ]);
      }
    }
    return $this->derivatives;
  }

}

Classes

Namesort descending Description
FieldBlockDeriver Provides block plugin definitions for fieldblock blocks.