Newer
Older
Angie Byron
committed
<?php
/**
* @file
* Contains \Drupal\block\Entity\Block.
Angie Byron
committed
*/
namespace Drupal\block\Entity;
Angie Byron
committed
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Entity\Annotation\EntityType;
Angie Byron
committed
use Drupal\Core\Annotation\Translation;
use Drupal\block\BlockPluginBag;
use Drupal\block\BlockInterface;
use Drupal\Core\Entity\EntityStorageControllerInterface;
Angie Byron
committed
/**
* Defines a Block configuration entity class.
*
* @EntityType(
Angie Byron
committed
* id = "block",
* label = @Translation("Block"),
* controllers = {
catch
committed
* "storage" = "Drupal\Core\Config\Entity\ConfigStorageController",
* "access" = "Drupal\block\BlockAccessController",
Alex Pott
committed
* "view_builder" = "Drupal\block\BlockViewBuilder",
* "list" = "Drupal\block\BlockListController",
* "form" = {
* "default" = "Drupal\block\BlockFormController",
* "delete" = "Drupal\block\Form\BlockDeleteForm"
Angie Byron
committed
* },
Angie Byron
committed
* config_prefix = "block.block",
Angie Byron
committed
* fieldable = FALSE,
* entity_keys = {
* "id" = "id",
* "label" = "label",
* "uuid" = "uuid"
* },
* links = {
* "edit-form" = "block.admin_edit"
Angie Byron
committed
* }
* )
*/
class Block extends ConfigEntityBase implements BlockInterface {
Angie Byron
committed
/**
* The ID of the block.
*
* @var string
*/
public $id;
/**
* The block UUID.
*
* @var string
*/
public $uuid;
/**
* The plugin instance settings.
*
* @var array
*/
protected $settings = array();
/**
* The region this block is placed in.
*
* @var string
*/
Alex Pott
committed
protected $region = self::BLOCK_REGION_NONE;
Angie Byron
committed
/**
* The block weight.
Angie Byron
committed
*
* @var int
Angie Byron
committed
*/
public $weight;
Angie Byron
committed
/**
* The plugin instance ID.
Angie Byron
committed
*
* @var string
Angie Byron
committed
*/
protected $plugin;
Angie Byron
committed
/**
* The plugin bag that holds the block plugin for this entity.
Angie Byron
committed
*
* @var \Drupal\block\BlockPluginBag
Angie Byron
committed
*/
protected $pluginBag;
Angie Byron
committed
/**
* The visibility settings.
Angie Byron
committed
*
* @var array
Angie Byron
committed
*/
protected $visibility;
/**
* Overrides \Drupal\Core\Config\Entity\ConfigEntityBase::__construct();
*/
public function __construct(array $values, $entity_type) {
parent::__construct($values, $entity_type);
$this->pluginBag = new BlockPluginBag(\Drupal::service('plugin.manager.block'), array($this->plugin), $this->get('settings'), $this->id());
}
Angie Byron
committed
/**
* {@inheritdoc}
Angie Byron
committed
*/
public function getPlugin() {
return $this->pluginBag->get($this->plugin);
Angie Byron
committed
}
/**
* Overrides \Drupal\Core\Entity\Entity::label();
*/
public function label($langcode = NULL) {
$settings = $this->get('settings');
Angie Byron
committed
if ($settings['label']) {
return $settings['label'];
}
else {
$definition = $this->getPlugin()->getPluginDefinition();
return $definition['admin_label'];
}
}
Angie Byron
committed
/**
* Overrides \Drupal\Core\Config\Entity\ConfigEntityBase::getExportProperties();
*/
public function getExportProperties() {
$properties = parent::getExportProperties();
Angie Byron
committed
$names = array(
'theme',
Angie Byron
committed
'region',
'weight',
'plugin',
'settings',
'visibility',
Angie Byron
committed
);
foreach ($names as $name) {
$properties[$name] = $this->get($name);
}
return $properties;
}
/**
* {@inheritdoc}
*/
public function preSave(EntityStorageControllerInterface $storage_controller) {
Angie Byron
committed
parent::preSave($storage_controller);
$this->set('settings', $this->getPlugin()->getConfiguration());
}
Alex Pott
committed
/**
* Sorts active blocks by weight; sorts inactive blocks by name.
*/
public static function sort($a, $b) {
// Separate enabled from disabled.
$status = $b->get('status') - $a->get('status');
if ($status) {
return $status;
}
// Sort by weight, unless disabled.
Alex Pott
committed
if ($a->get('region') != static::BLOCK_REGION_NONE) {
Alex Pott
committed
$weight = $a->get('weight') - $b->get('weight');
if ($weight) {
return $weight;
}
}
// Sort by label.
return strcmp($a->label(), $b->label());
}