UnboundDisplay.php 1.59 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
<?php

/**
 * @file
 * Definition of Drupal\layout\Plugin\Core\Entity\Display.
 */

namespace Drupal\layout\Plugin\Core\Entity;

use Drupal\layout\Config\DisplayBase;
use Drupal\layout\Config\BoundDisplayInterface;
use Drupal\layout\Config\UnboundDisplayInterface;
use Drupal\layout\Plugin\LayoutInterface;
14
use Drupal\Core\Entity\Annotation\EntityType;
15 16 17 18 19 20 21 22
use Drupal\Core\Annotation\Translation;

/**
 * Defines the unbound_display entity.
 *
 * Unbound displays contain blocks that are not 'bound' to a specific layout,
 * and their contained blocks are mapped only to region types, not regions.
 *
23
 * @EntityType(
24 25 26
 *   id = "unbound_display",
 *   label = @Translation("Unbound Display"),
 *   module = "layout",
27 28 29
 *   controllers = {
 *     "storage" = "Drupal\Core\Config\Entity\ConfigStorageController"
 *   },
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
 *   config_prefix = "display.unbound",
 *   entity_keys = {
 *     "id" = "id",
 *     "uuid" = "uuid"
 *   }
 * )
 */
class UnboundDisplay extends DisplayBase implements UnboundDisplayInterface {

  /**
   * Implements UnboundDisplayInterface::generateDisplay().
   *
   * @throws \Exception
   */
  public function generateDisplay(LayoutInterface $layout, $id, $entity_type = 'display') {
    $values = array(
      'layout' => $layout->getPluginId(),
      'blockInfo' => $this->mapBlocksToLayout($layout),
      'id' => $id,
    );

    $entity = entity_create($entity_type, $values);

    if (!$entity instanceof BoundDisplayInterface) {
      throw new \Exception(sprintf('Attempted to bind an unbound display but provided an invalid entity type.'), E_RECOVERABLE_ERROR);
    }

    return $entity;
  }
}