3 namespace Drupal\block\Entity;
5 use Drupal\Core\Cache\Cache;
6 use Drupal\Core\Condition\ConditionPluginCollection;
7 use Drupal\Core\Config\Entity\ConfigEntityBase;
8 use Drupal\block\BlockPluginCollection;
9 use Drupal\block\BlockInterface;
10 use Drupal\Core\Config\Entity\ConfigEntityInterface;
11 use Drupal\Core\Entity\EntityWithPluginCollectionInterface;
12 use Drupal\Core\Entity\EntityStorageInterface;
15 * Defines a Block configuration entity class.
19 * label = @Translation("Block"),
21 * "access" = "Drupal\block\BlockAccessControlHandler",
22 * "view_builder" = "Drupal\block\BlockViewBuilder",
23 * "list_builder" = "Drupal\block\BlockListBuilder",
25 * "default" = "Drupal\block\BlockForm",
26 * "delete" = "Drupal\block\Form\BlockDeleteForm"
29 * admin_permission = "administer blocks",
35 * "delete-form" = "/admin/structure/block/manage/{block}/delete",
36 * "edit-form" = "/admin/structure/block/manage/{block}",
37 * "enable" = "/admin/structure/block/manage/{block}/enable",
38 * "disable" = "/admin/structure/block/manage/{block}/disable",
55 class Block extends ConfigEntityBase implements BlockInterface, EntityWithPluginCollectionInterface {
58 * The ID of the block.
65 * The plugin instance settings.
69 protected $settings = [];
72 * The region this block is placed in.
86 * The plugin instance ID.
93 * The visibility settings for this block.
97 protected $visibility = [];
100 * The plugin collection that holds the block plugin for this entity.
102 * @var \Drupal\block\BlockPluginCollection
104 protected $pluginCollection;
107 * The available contexts for this block and its visibility conditions.
111 protected $contexts = [];
114 * The visibility collection.
116 * @var \Drupal\Core\Condition\ConditionPluginCollection
118 protected $visibilityCollection;
121 * The condition plugin manager.
123 * @var \Drupal\Core\Executable\ExecutableManagerInterface
125 protected $conditionPluginManager;
128 * The theme that includes the block plugin for this entity.
137 public function getPlugin() {
138 return $this->getPluginCollection()->get($this->plugin);
142 * Encapsulates the creation of the block's LazyPluginCollection.
144 * @return \Drupal\Component\Plugin\LazyPluginCollection
145 * The block's plugin collection.
147 protected function getPluginCollection() {
148 if (!$this->pluginCollection) {
149 $this->pluginCollection = new BlockPluginCollection(\Drupal::service('plugin.manager.block'), $this->plugin, $this->get('settings'), $this->id());
151 return $this->pluginCollection;
157 public function getPluginCollections() {
159 'settings' => $this->getPluginCollection(),
160 'visibility' => $this->getVisibilityConditions(),
167 public function getPluginId() {
168 return $this->plugin;
174 public function getRegion() {
175 return $this->region;
181 public function getTheme() {
188 public function getWeight() {
189 return $this->weight;
195 public function label() {
196 $settings = $this->get('settings');
197 if ($settings['label']) {
198 return $settings['label'];
201 $definition = $this->getPlugin()->getPluginDefinition();
202 return $definition['admin_label'];
207 * Sorts active blocks by weight; sorts inactive blocks by name.
209 public static function sort(ConfigEntityInterface $a, ConfigEntityInterface $b) {
210 // Separate enabled from disabled.
211 $status = (int) $b->status() - (int) $a->status();
217 $weight = $a->getWeight() - $b->getWeight();
223 return strcmp($a->label(), $b->label());
229 public function calculateDependencies() {
230 parent::calculateDependencies();
231 $this->addDependency('theme', $this->theme);
238 public function postSave(EntityStorageInterface $storage, $update = TRUE) {
239 parent::postSave($storage, $update);
241 // Entity::postSave() calls Entity::invalidateTagsOnSave(), which only
242 // handles the regular cases. The Block entity has one special case: a
243 // newly created block may *also* appear on any page in the current theme,
244 // so we must invalidate the associated block's cache tag (which includes
245 // the theme cache tag).
247 Cache::invalidateTags($this->getCacheTagsToInvalidate());
254 public function getVisibility() {
255 return $this->getVisibilityConditions()->getConfiguration();
261 public function setVisibilityConfig($instance_id, array $configuration) {
262 $conditions = $this->getVisibilityConditions();
263 if (!$conditions->has($instance_id)) {
264 $configuration['id'] = $instance_id;
265 $conditions->addInstanceId($instance_id, $configuration);
268 $conditions->setInstanceConfiguration($instance_id, $configuration);
276 public function getVisibilityConditions() {
277 if (!isset($this->visibilityCollection)) {
278 $this->visibilityCollection = new ConditionPluginCollection($this->conditionPluginManager(), $this->get('visibility'));
280 return $this->visibilityCollection;
286 public function getVisibilityCondition($instance_id) {
287 return $this->getVisibilityConditions()->get($instance_id);
291 * Gets the condition plugin manager.
293 * @return \Drupal\Core\Executable\ExecutableManagerInterface
294 * The condition plugin manager.
296 protected function conditionPluginManager() {
297 if (!isset($this->conditionPluginManager)) {
298 $this->conditionPluginManager = \Drupal::service('plugin.manager.condition');
300 return $this->conditionPluginManager;
306 public function setRegion($region) {
307 $this->region = $region;
314 public function setWeight($weight) {
315 $this->weight = $weight;
322 public function createDuplicateBlock($new_id = NULL, $new_theme = NULL) {
323 $duplicate = parent::createDuplicate();
324 if (!empty($new_id)) {
325 $duplicate->id = $new_id;
327 if (!empty($new_theme)) {
328 $duplicate->theme = $new_theme;
336 public function preSave(EntityStorageInterface $storage) {
337 parent::preSave($storage);
339 // Ensure the region is valid to mirror the behavior of block_rebuild().
340 // This is done primarily for backwards compatibility support of
341 // \Drupal\block\BlockInterface::BLOCK_REGION_NONE.
342 $regions = system_region_list($this->theme);
343 if (!isset($regions[$this->region]) && $this->status()) {
345 ->setRegion(system_default_region($this->theme))