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"),
20 * label_collection = @Translation("Blocks"),
21 * label_singular = @Translation("block"),
22 * label_plural = @Translation("blocks"),
23 * label_count = @PluralTranslation(
24 * singular = "@count block",
25 * plural = "@count blocks",
28 * "access" = "Drupal\block\BlockAccessControlHandler",
29 * "view_builder" = "Drupal\block\BlockViewBuilder",
30 * "list_builder" = "Drupal\block\BlockListBuilder",
32 * "default" = "Drupal\block\BlockForm",
33 * "delete" = "Drupal\block\Form\BlockDeleteForm"
36 * admin_permission = "administer blocks",
42 * "delete-form" = "/admin/structure/block/manage/{block}/delete",
43 * "edit-form" = "/admin/structure/block/manage/{block}",
44 * "enable" = "/admin/structure/block/manage/{block}/enable",
45 * "disable" = "/admin/structure/block/manage/{block}/disable",
62 class Block extends ConfigEntityBase implements BlockInterface, EntityWithPluginCollectionInterface {
65 * The ID of the block.
72 * The plugin instance settings.
76 protected $settings = [];
79 * The region this block is placed in.
93 * The plugin instance ID.
100 * The visibility settings for this block.
104 protected $visibility = [];
107 * The plugin collection that holds the block plugin for this entity.
109 * @var \Drupal\block\BlockPluginCollection
111 protected $pluginCollection;
114 * The available contexts for this block and its visibility conditions.
118 protected $contexts = [];
121 * The visibility collection.
123 * @var \Drupal\Core\Condition\ConditionPluginCollection
125 protected $visibilityCollection;
128 * The condition plugin manager.
130 * @var \Drupal\Core\Executable\ExecutableManagerInterface
132 protected $conditionPluginManager;
135 * The theme that includes the block plugin for this entity.
144 public function getPlugin() {
145 return $this->getPluginCollection()->get($this->plugin);
149 * Encapsulates the creation of the block's LazyPluginCollection.
151 * @return \Drupal\Component\Plugin\LazyPluginCollection
152 * The block's plugin collection.
154 protected function getPluginCollection() {
155 if (!$this->pluginCollection) {
156 $this->pluginCollection = new BlockPluginCollection(\Drupal::service('plugin.manager.block'), $this->plugin, $this->get('settings'), $this->id());
158 return $this->pluginCollection;
164 public function getPluginCollections() {
166 'settings' => $this->getPluginCollection(),
167 'visibility' => $this->getVisibilityConditions(),
174 public function getPluginId() {
175 return $this->plugin;
181 public function getRegion() {
182 return $this->region;
188 public function getTheme() {
195 public function getWeight() {
196 return $this->weight;
202 public function label() {
203 $settings = $this->get('settings');
204 if ($settings['label']) {
205 return $settings['label'];
208 $definition = $this->getPlugin()->getPluginDefinition();
209 return $definition['admin_label'];
214 * Sorts active blocks by weight; sorts inactive blocks by name.
216 public static function sort(ConfigEntityInterface $a, ConfigEntityInterface $b) {
217 // Separate enabled from disabled.
218 $status = (int) $b->status() - (int) $a->status();
224 $weight = $a->getWeight() - $b->getWeight();
230 return strcmp($a->label(), $b->label());
236 public function calculateDependencies() {
237 parent::calculateDependencies();
238 $this->addDependency('theme', $this->theme);
245 public function postSave(EntityStorageInterface $storage, $update = TRUE) {
246 parent::postSave($storage, $update);
248 // Entity::postSave() calls Entity::invalidateTagsOnSave(), which only
249 // handles the regular cases. The Block entity has one special case: a
250 // newly created block may *also* appear on any page in the current theme,
251 // so we must invalidate the associated block's cache tag (which includes
252 // the theme cache tag).
254 Cache::invalidateTags($this->getCacheTagsToInvalidate());
261 public function getVisibility() {
262 return $this->getVisibilityConditions()->getConfiguration();
268 public function setVisibilityConfig($instance_id, array $configuration) {
269 $conditions = $this->getVisibilityConditions();
270 if (!$conditions->has($instance_id)) {
271 $configuration['id'] = $instance_id;
272 $conditions->addInstanceId($instance_id, $configuration);
275 $conditions->setInstanceConfiguration($instance_id, $configuration);
283 public function getVisibilityConditions() {
284 if (!isset($this->visibilityCollection)) {
285 $this->visibilityCollection = new ConditionPluginCollection($this->conditionPluginManager(), $this->get('visibility'));
287 return $this->visibilityCollection;
293 public function getVisibilityCondition($instance_id) {
294 return $this->getVisibilityConditions()->get($instance_id);
298 * Gets the condition plugin manager.
300 * @return \Drupal\Core\Executable\ExecutableManagerInterface
301 * The condition plugin manager.
303 protected function conditionPluginManager() {
304 if (!isset($this->conditionPluginManager)) {
305 $this->conditionPluginManager = \Drupal::service('plugin.manager.condition');
307 return $this->conditionPluginManager;
313 public function setRegion($region) {
314 $this->region = $region;
321 public function setWeight($weight) {
322 $this->weight = $weight;
329 public function createDuplicateBlock($new_id = NULL, $new_theme = NULL) {
330 $duplicate = parent::createDuplicate();
331 if (!empty($new_id)) {
332 $duplicate->id = $new_id;
334 if (!empty($new_theme)) {
335 $duplicate->theme = $new_theme;
343 public function preSave(EntityStorageInterface $storage) {
344 parent::preSave($storage);
346 // Ensure the region is valid to mirror the behavior of block_rebuild().
347 // This is done primarily for backwards compatibility support of
348 // \Drupal\block\BlockInterface::BLOCK_REGION_NONE.
349 $regions = system_region_list($this->theme);
350 if (!isset($regions[$this->region]) && $this->status()) {
352 ->setRegion(system_default_region($this->theme))