You are here

class DynamicLayoutDeriver in Dynamic Layouts 8

Makes a flexible layout for each layout config entity.

Hierarchy

Expanded class hierarchy of DynamicLayoutDeriver

File

src/Plugin/Derivative/DynamicLayoutDeriver.php, line 14

Namespace

Drupal\dynamic_layouts\Plugin\Derivative
View source
class DynamicLayoutDeriver extends DeriverBase implements ContainerDeriverInterface {

  /**
   * The base plugin ID that the derivative is for.
   *
   * @var string
   */
  protected $basePluginId;

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

  /**
   * DynamicLayoutDeriver constructor.
   *
   * @param string $base_plugin_id
   *   The base plugin ID.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   The entity type manager.
   */
  public function __construct($base_plugin_id, EntityTypeManagerInterface $entityTypeManager) {
    $this->basePluginId = $base_plugin_id;
    $this->entityTypeManager = $entityTypeManager;
  }

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

  /**
   * {@inheritdoc}
   */
  public function getDerivativeDefinitions($base_plugin_definition) {
    $config_entities = $this->entityTypeManager
      ->getStorage('dynamic_layout')
      ->loadMultiple();

    // Now we loop over them and declare the derivatives.
    if ($config_entities) {
      foreach ($config_entities as $entity) {

        /* @var \Drupal\dynamic_layouts\DynamicLayoutInterface $entity */
        $regions = $entity
          ->getLayoutRegions();
        $icon_map = $entity
          ->getIconMap();
        $module_path = drupal_get_path('module', 'dynamic_layouts') . '/templates';
        $this->derivatives[$entity
          ->id()] = new LayoutDefinition([
          'label' => $entity
            ->label(),
          'category' => $entity
            ->getCategory(),
          'class' => 'Drupal\\dynamic_layouts\\Plugin\\Layout\\DynamicLayout',
          'regions' => $regions,
          'template' => 'dynamic-layout-frontend',
          'path' => $module_path,
          'icon_map' => $icon_map,
          'config_dependencies' => [
            $entity
              ->getConfigDependencyKey() => [
              $entity
                ->getConfigDependencyName(),
            ],
          ],
        ]);
      }
    }
    return $this->derivatives;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DeriverBase::$derivatives protected property List of derivative definitions. 1
DeriverBase::getDerivativeDefinition public function Gets the definition of a derivative plugin. Overrides DeriverInterface::getDerivativeDefinition
DynamicLayoutDeriver::$basePluginId protected property The base plugin ID that the derivative is for.
DynamicLayoutDeriver::$entityTypeManager protected property The entity type manager.
DynamicLayoutDeriver::create public static function Creates a new class instance. Overrides ContainerDeriverInterface::create
DynamicLayoutDeriver::getDerivativeDefinitions public function Gets the definition of all derivatives of a base plugin. Overrides DeriverBase::getDerivativeDefinitions
DynamicLayoutDeriver::__construct public function DynamicLayoutDeriver constructor.