Same filename in this branch
- 10 core/modules/block/src/Entity/Block.php
- 10 core/lib/Drupal/Core/Block/Annotation/Block.php
- 10 core/modules/block/src/Plugin/migrate/source/Block.php
- 10 core/modules/views/src/Plugin/views/display/Block.php
- 10 core/lib/Drupal/Core/Block/Attribute/Block.php
Same filename and directory in other branches
- 8.9.x core/modules/block/src/Entity/Block.php
- 9 core/modules/block/src/Entity/Block.php
Namespace
Drupal\block\Entity
File
core/modules/block/src/Entity/Block.php
View source
<?php
namespace Drupal\block\Entity;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Condition\ConditionPluginCollection;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\block\BlockPluginCollection;
use Drupal\block\BlockInterface;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Entity\EntityWithPluginCollectionInterface;
use Drupal\Core\Entity\EntityStorageInterface;
class Block extends ConfigEntityBase implements BlockInterface, EntityWithPluginCollectionInterface {
protected $id;
protected $settings = [];
protected $region;
protected $weight;
protected $plugin;
protected $visibility = [];
protected $pluginCollection;
protected $contexts = [];
protected $visibilityCollection;
protected $conditionPluginManager;
protected $theme;
public function getPlugin() {
return $this
->getPluginCollection()
->get($this->plugin);
}
protected function getPluginCollection() {
if (!$this->pluginCollection) {
$this->pluginCollection = new BlockPluginCollection(\Drupal::service('plugin.manager.block'), $this->plugin, $this
->get('settings'), $this
->id());
}
return $this->pluginCollection;
}
public function getPluginCollections() {
return [
'settings' => $this
->getPluginCollection(),
'visibility' => $this
->getVisibilityConditions(),
];
}
public function getPluginId() {
return $this->plugin;
}
public function getRegion() {
return $this->region;
}
public function getTheme() {
return $this->theme;
}
public function getWeight() {
return $this->weight;
}
public function label() {
$settings = $this
->get('settings');
if ($settings['label']) {
return $settings['label'];
}
else {
$definition = $this
->getPlugin()
->getPluginDefinition();
return $definition['admin_label'];
}
}
public static function sort(ConfigEntityInterface $a, ConfigEntityInterface $b) {
$status = (int) $b
->status() - (int) $a
->status();
if ($status !== 0) {
return $status;
}
$weight = $a
->getWeight() - $b
->getWeight();
if ($weight) {
return $weight;
}
return strcmp($a
->label(), $b
->label());
}
public function calculateDependencies() {
parent::calculateDependencies();
$this
->addDependency('theme', $this->theme);
return $this;
}
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update);
if (!$update) {
Cache::invalidateTags($this
->getCacheTagsToInvalidate());
}
}
public function getVisibility() {
return $this
->getVisibilityConditions()
->getConfiguration();
}
public function setVisibilityConfig($instance_id, array $configuration) {
$conditions = $this
->getVisibilityConditions();
if (!$conditions
->has($instance_id)) {
$configuration['id'] = $instance_id;
$conditions
->addInstanceId($instance_id, $configuration);
}
else {
$conditions
->setInstanceConfiguration($instance_id, $configuration);
}
return $this;
}
public function getVisibilityConditions() {
if (!isset($this->visibilityCollection)) {
$this->visibilityCollection = new ConditionPluginCollection($this
->conditionPluginManager(), $this
->get('visibility'));
}
return $this->visibilityCollection;
}
public function getVisibilityCondition($instance_id) {
return $this
->getVisibilityConditions()
->get($instance_id);
}
protected function conditionPluginManager() {
if (!isset($this->conditionPluginManager)) {
$this->conditionPluginManager = \Drupal::service('plugin.manager.condition');
}
return $this->conditionPluginManager;
}
public function setRegion($region) {
$this->region = $region;
return $this;
}
public function setWeight($weight) {
$this->weight = $weight;
return $this;
}
public function createDuplicateBlock($new_id = NULL, $new_theme = NULL) {
$duplicate = parent::createDuplicate();
if (!empty($new_id)) {
$duplicate->id = $new_id;
}
if (!empty($new_theme)) {
$duplicate->theme = $new_theme;
}
return $duplicate;
}
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
$regions = system_region_list($this->theme);
if (!isset($regions[$this->region]) && $this
->status()) {
$this
->setRegion(system_default_region($this->theme))
->disable();
}
}
}
Classes
Name |
Description |
Block |
Defines a Block configuration entity class. |