class Block in Drupal 10
Same name in this branch
- 10 core/modules/block/src/Entity/Block.php \Drupal\block\Entity\Block
- 10 core/lib/Drupal/Core/Block/Annotation/Block.php \Drupal\Core\Block\Annotation\Block
- 10 core/modules/views/src/Plugin/views/display/Block.php \Drupal\views\Plugin\views\display\Block
- 10 core/modules/block/src/Plugin/migrate/source/Block.php \Drupal\block\Plugin\migrate\source\Block
Same name and namespace in other branches
- 8 core/modules/block/src/Entity/Block.php \Drupal\block\Entity\Block
- 9 core/modules/block/src/Entity/Block.php \Drupal\block\Entity\Block
Defines a Block configuration entity class.
Plugin annotation
@ConfigEntityType(
id = "block",
label = @Translation("Block"),
label_collection = @Translation("Blocks"),
label_singular = @Translation("block"),
label_plural = @Translation("blocks"),
label_count = @PluralTranslation(
singular = "@count block",
plural = "@count blocks",
),
handlers = {
"access" = "Drupal\block\BlockAccessControlHandler",
"view_builder" = "Drupal\block\BlockViewBuilder",
"list_builder" = "Drupal\block\BlockListBuilder",
"form" = {
"default" = "Drupal\block\BlockForm",
"delete" = "Drupal\block\Form\BlockDeleteForm"
}
},
admin_permission = "administer blocks",
entity_keys = {
"id" = "id",
"status" = "status"
},
links = {
"delete-form" = "/admin/structure/block/manage/{block}/delete",
"edit-form" = "/admin/structure/block/manage/{block}",
"enable" = "/admin/structure/block/manage/{block}/enable",
"disable" = "/admin/structure/block/manage/{block}/disable",
},
config_export = {
"id",
"theme",
"region",
"weight",
"provider",
"plugin",
"settings",
"visibility",
},
lookup_keys = {
"theme"
}
)
Hierarchy
- class \Drupal\Core\Entity\EntityBase implements EntityInterface uses RefinableCacheableDependencyTrait, DependencySerializationTrait
- class \Drupal\Core\Config\Entity\ConfigEntityBase implements ConfigEntityInterface uses SynchronizableEntityTrait, PluginDependencyTrait
- class \Drupal\block\Entity\Block implements BlockInterface, EntityWithPluginCollectionInterface
- class \Drupal\Core\Config\Entity\ConfigEntityBase implements ConfigEntityInterface uses SynchronizableEntityTrait, PluginDependencyTrait
Expanded class hierarchy of Block
31 files declare their use of Block
- AreaEntityUITest.php in core/
modules/ views_ui/ tests/ src/ Functional/ AreaEntityUITest.php - AssertBlockAppearsTrait.php in core/
modules/ block/ tests/ src/ Functional/ AssertBlockAppearsTrait.php - block.module in core/
modules/ block/ block.module - Controls the visual building blocks a page is constructed with.
- BlockConfigSchemaTest.php in core/
modules/ block/ tests/ src/ Kernel/ BlockConfigSchemaTest.php - BlockCreationTrait.php in core/
modules/ block/ tests/ src/ Traits/ BlockCreationTrait.php
43 string references to 'Block'
- block.info.yml in core/
modules/ block/ block.info.yml - core/modules/block/block.info.yml
- block.schema.yml in core/
modules/ block/ config/ schema/ block.schema.yml - core/modules/block/config/schema/block.schema.yml
- BlockLibraryController::listBlocks in core/
modules/ block/ src/ Controller/ BlockLibraryController.php - Shows a list of blocks that can be added to a theme's layout.
- BlockListBuilder::buildBlocksForm in core/
modules/ block/ src/ BlockListBuilder.php - Builds the main "Blocks" portion of the form.
- BlockManagerTest::setUp in core/
tests/ Drupal/ Tests/ Core/ Block/ BlockManagerTest.php
File
- core/
modules/ block/ src/ Entity/ Block.php, line 62
Namespace
Drupal\block\EntityView source
class Block extends ConfigEntityBase implements BlockInterface, EntityWithPluginCollectionInterface {
/**
* The ID of the block.
*
* @var string
*/
protected $id;
/**
* The plugin instance settings.
*
* @var array
*/
protected $settings = [];
/**
* The region this block is placed in.
*
* @var string
*/
protected $region;
/**
* The block weight.
*
* @var int
*/
protected $weight;
/**
* The plugin instance ID.
*
* @var string
*/
protected $plugin;
/**
* The visibility settings for this block.
*
* @var array
*/
protected $visibility = [];
/**
* The plugin collection that holds the block plugin for this entity.
*
* @var \Drupal\block\BlockPluginCollection
*/
protected $pluginCollection;
/**
* The available contexts for this block and its visibility conditions.
*
* @var array
*/
protected $contexts = [];
/**
* The visibility collection.
*
* @var \Drupal\Core\Condition\ConditionPluginCollection
*/
protected $visibilityCollection;
/**
* The condition plugin manager.
*
* @var \Drupal\Core\Executable\ExecutableManagerInterface
*/
protected $conditionPluginManager;
/**
* The theme that includes the block plugin for this entity.
*
* @var string
*/
protected $theme;
/**
* {@inheritdoc}
*/
public function getPlugin() {
return $this
->getPluginCollection()
->get($this->plugin);
}
/**
* Encapsulates the creation of the block's LazyPluginCollection.
*
* @return \Drupal\Component\Plugin\LazyPluginCollection
* The block's plugin collection.
*/
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;
}
/**
* {@inheritdoc}
*/
public function getPluginCollections() {
return [
'settings' => $this
->getPluginCollection(),
'visibility' => $this
->getVisibilityConditions(),
];
}
/**
* {@inheritdoc}
*/
public function getPluginId() {
return $this->plugin;
}
/**
* {@inheritdoc}
*/
public function getRegion() {
return $this->region;
}
/**
* {@inheritdoc}
*/
public function getTheme() {
return $this->theme;
}
/**
* {@inheritdoc}
*/
public function getWeight() {
return $this->weight;
}
/**
* {@inheritdoc}
*/
public function label() {
$settings = $this
->get('settings');
if ($settings['label']) {
return $settings['label'];
}
else {
$definition = $this
->getPlugin()
->getPluginDefinition();
return $definition['admin_label'];
}
}
/**
* Sorts active blocks by weight; sorts inactive blocks by name.
*/
public static function sort(ConfigEntityInterface $a, ConfigEntityInterface $b) {
// Separate enabled from disabled.
$status = (int) $b
->status() - (int) $a
->status();
if ($status !== 0) {
return $status;
}
// Sort by weight.
$weight = $a
->getWeight() - $b
->getWeight();
if ($weight) {
return $weight;
}
// Sort by label.
return strcmp($a
->label(), $b
->label());
}
/**
* {@inheritdoc}
*/
public function calculateDependencies() {
parent::calculateDependencies();
$this
->addDependency('theme', $this->theme);
return $this;
}
/**
* {@inheritdoc}
*/
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update);
// Entity::postSave() calls Entity::invalidateTagsOnSave(), which only
// handles the regular cases. The Block entity has one special case: a
// newly created block may *also* appear on any page in the current theme,
// so we must invalidate the associated block's cache tag (which includes
// the theme cache tag).
if (!$update) {
Cache::invalidateTags($this
->getCacheTagsToInvalidate());
}
}
/**
* {@inheritdoc}
*/
public function getVisibility() {
return $this
->getVisibilityConditions()
->getConfiguration();
}
/**
* {@inheritdoc}
*/
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;
}
/**
* {@inheritdoc}
*/
public function getVisibilityConditions() {
if (!isset($this->visibilityCollection)) {
$this->visibilityCollection = new ConditionPluginCollection($this
->conditionPluginManager(), $this
->get('visibility'));
}
return $this->visibilityCollection;
}
/**
* {@inheritdoc}
*/
public function getVisibilityCondition($instance_id) {
return $this
->getVisibilityConditions()
->get($instance_id);
}
/**
* Gets the condition plugin manager.
*
* @return \Drupal\Core\Executable\ExecutableManagerInterface
* The condition plugin manager.
*/
protected function conditionPluginManager() {
if (!isset($this->conditionPluginManager)) {
$this->conditionPluginManager = \Drupal::service('plugin.manager.condition');
}
return $this->conditionPluginManager;
}
/**
* {@inheritdoc}
*/
public function setRegion($region) {
$this->region = $region;
return $this;
}
/**
* {@inheritdoc}
*/
public function setWeight($weight) {
$this->weight = $weight;
return $this;
}
/**
* {@inheritdoc}
*/
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;
}
/**
* {@inheritdoc}
*/
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
// Ensure the region is valid to mirror the behavior of block_rebuild().
// This is done primarily for backwards compatibility support of
// \Drupal\block\BlockInterface::BLOCK_REGION_NONE.
$regions = system_region_list($this->theme);
if (!isset($regions[$this->region]) && $this
->status()) {
$this
->setRegion(system_default_region($this->theme))
->disable();
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
Block:: |
protected | property | The condition plugin manager. | |
Block:: |
protected | property | The available contexts for this block and its visibility conditions. | |
Block:: |
protected | property | The ID of the block. | |
Block:: |
protected | property | The plugin instance ID. | |
Block:: |
protected | property | The plugin collection that holds the block plugin for this entity. | |
Block:: |
protected | property | The region this block is placed in. | |
Block:: |
protected | property | The plugin instance settings. | |
Block:: |
protected | property | The theme that includes the block plugin for this entity. | |
Block:: |
protected | property | The visibility settings for this block. | |
Block:: |
protected | property | The visibility collection. | |
Block:: |
protected | property | The block weight. | |
Block:: |
public | function |
Calculates dependencies and stores them in the dependency property. Overrides ConfigEntityBase:: |
|
Block:: |
protected | function | Gets the condition plugin manager. | |
Block:: |
public | function |
Creates a duplicate of the block entity. Overrides BlockInterface:: |
|
Block:: |
public | function |
Returns the plugin instance. Overrides BlockInterface:: |
|
Block:: |
protected | function | Encapsulates the creation of the block's LazyPluginCollection. | |
Block:: |
public | function |
Gets the plugin collections used by this object. Overrides ObjectWithPluginCollectionInterface:: |
|
Block:: |
public | function |
Returns the plugin ID. Overrides BlockInterface:: |
|
Block:: |
public | function |
Returns the region this block is placed in. Overrides BlockInterface:: |
|
Block:: |
public | function |
Returns the theme ID. Overrides BlockInterface:: |
|
Block:: |
public | function |
Returns an array of visibility condition configurations. Overrides BlockInterface:: |
|
Block:: |
public | function |
Gets a visibility condition plugin instance. Overrides BlockInterface:: |
|
Block:: |
public | function |
Gets conditions for this block. Overrides BlockInterface:: |
|
Block:: |
public | function |
Returns the weight of this block (used for sorting). Overrides BlockInterface:: |
|
Block:: |
public | function |
Gets the label of the entity. Overrides EntityBase:: |
|
Block:: |
public | function |
Acts on a saved entity before the insert or update hook is invoked. Overrides EntityBase:: |
|
Block:: |
public | function |
Acts on an entity before the presave hook is invoked. Overrides ConfigEntityBase:: |
|
Block:: |
public | function |
Sets the region this block is placed in. Overrides BlockInterface:: |
|
Block:: |
public | function |
Sets the visibility condition configuration. Overrides BlockInterface:: |
|
Block:: |
public | function |
Sets the block weight. Overrides BlockInterface:: |
|
Block:: |
public static | function |
Sorts active blocks by weight; sorts inactive blocks by name. Overrides ConfigEntityBase:: |
|
CacheableDependencyTrait:: |
protected | property | Cache contexts. | |
CacheableDependencyTrait:: |
protected | property | Cache max-age. | |
CacheableDependencyTrait:: |
protected | property | Cache tags. | |
CacheableDependencyTrait:: |
protected | function | Sets cacheability; useful for value object constructors. | |
ConfigEntityBase:: |
private | property | Whether the config is being deleted by the uninstall process. | |
ConfigEntityBase:: |
protected | property | The language code of the entity's default language. | |
ConfigEntityBase:: |
protected | property | The original ID of the configuration entity. | |
ConfigEntityBase:: |
protected | property | The enabled/disabled status of the configuration entity. | 4 |
ConfigEntityBase:: |
protected | property | Third party entity settings. | |
ConfigEntityBase:: |
protected | property | Trust supplied data and not use configuration schema on save. | |
ConfigEntityBase:: |
protected | property | The UUID for this entity. | |
ConfigEntityBase:: |
protected | property | ||
ConfigEntityBase:: |
protected | function | Overrides \Drupal\Core\Entity\DependencyTrait:addDependency(). | |
ConfigEntityBase:: |
public | function |
Creates a duplicate of the entity. Overrides EntityBase:: |
1 |
ConfigEntityBase:: |
public | function |
Disables the configuration entity. Overrides ConfigEntityInterface:: |
1 |
ConfigEntityBase:: |
public | function |
Enables the configuration entity. Overrides ConfigEntityInterface:: |
|
ConfigEntityBase:: |
public | function |
Returns the value of a property. Overrides ConfigEntityInterface:: |
|
ConfigEntityBase:: |
public | function |
Returns the cache tags that should be used to invalidate caches. Overrides EntityBase:: |
1 |
ConfigEntityBase:: |
public | function |
Gets the configuration dependency name. Overrides EntityBase:: |
|
ConfigEntityBase:: |
protected static | function | Gets the configuration manager. | |
ConfigEntityBase:: |
public | function |
Gets the configuration target identifier for the entity. Overrides EntityBase:: |
|
ConfigEntityBase:: |
public | function |
Gets the configuration dependencies. Overrides ConfigEntityInterface:: |
|
ConfigEntityBase:: |
public | function |
Gets the original ID. Overrides EntityBase:: |
|
ConfigEntityBase:: |
public | function |
Gets the list of third parties that store information. Overrides ThirdPartySettingsInterface:: |
|
ConfigEntityBase:: |
public | function |
Gets the value of a third-party setting. Overrides ThirdPartySettingsInterface:: |
|
ConfigEntityBase:: |
public | function |
Gets all third-party settings of a given module. Overrides ThirdPartySettingsInterface:: |
|
ConfigEntityBase:: |
protected | function | Gets the typed config manager. | |
ConfigEntityBase:: |
public | function |
Gets whether on not the data is trusted. Overrides ConfigEntityInterface:: |
|
ConfigEntityBase:: |
protected static | function |
Override to never invalidate the individual entities' cache tags; the
config system already invalidates them. Overrides EntityBase:: |
|
ConfigEntityBase:: |
protected | function |
Override to never invalidate the entity's cache tag; the config system
already invalidates it. Overrides EntityBase:: |
|
ConfigEntityBase:: |
public | function |
Checks whether this entity is installable. Overrides ConfigEntityInterface:: |
2 |
ConfigEntityBase:: |
public | function |
Overrides Entity::isNew(). Overrides EntityBase:: |
|
ConfigEntityBase:: |
public | function |
Returns whether this entity is being changed during the uninstall process. Overrides ConfigEntityInterface:: |
|
ConfigEntityBase:: |
public | function |
Informs the entity that entities it depends on will be deleted. Overrides ConfigEntityInterface:: |
8 |
ConfigEntityBase:: |
public static | function |
Acts on entities before they are deleted and before hooks are invoked. Overrides EntityBase:: |
6 |
ConfigEntityBase:: |
public | function |
Saves an entity permanently. Overrides EntityBase:: |
1 |
ConfigEntityBase:: |
public | function |
Sets the value of a property. Overrides ConfigEntityInterface:: |
|
ConfigEntityBase:: |
public | function |
Sets the original ID. Overrides EntityBase:: |
|
ConfigEntityBase:: |
public | function |
Sets the status of the configuration entity. Overrides ConfigEntityInterface:: |
|
ConfigEntityBase:: |
public | function |
Sets the value of a third-party setting. Overrides ThirdPartySettingsInterface:: |
|
ConfigEntityBase:: |
public | function | ||
ConfigEntityBase:: |
public | function |
Returns whether the configuration entity is enabled. Overrides ConfigEntityInterface:: |
4 |
ConfigEntityBase:: |
public | function |
Gets an array of all property values. Overrides EntityBase:: |
2 |
ConfigEntityBase:: |
public | function |
Gets the URL object for the entity. Overrides EntityBase:: |
|
ConfigEntityBase:: |
public | function |
Sets that the data should be trusted. Overrides ConfigEntityInterface:: |
|
ConfigEntityBase:: |
public | function |
Unsets a third-party setting. Overrides ThirdPartySettingsInterface:: |
|
ConfigEntityBase:: |
public | function |
Constructs an Entity object. Overrides EntityBase:: |
6 |
ConfigEntityBase:: |
public | function |
Overrides EntityBase:: |
3 |
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | Aliased as: traitSleep | 2 |
DependencySerializationTrait:: |
public | function | 2 | |
DependencyTrait:: |
protected | property | The object's dependencies. | |
DependencyTrait:: |
protected | function | Adds multiple dependencies. | |
DependencyTrait:: |
protected | function | Adds a dependency. Aliased as: addDependencyTrait | |
EntityBase:: |
protected | property | Boolean indicating whether the entity should be forced to be new. | |
EntityBase:: |
protected | property | The entity type. | |
EntityBase:: |
protected | property | A typed data object wrapping this entity. | |
EntityBase:: |
public | function | 1 | |
EntityBase:: |
public | function | 1 | |
EntityBase:: |
public static | function | 1 | |
EntityBase:: |
public | function | 2 | |
EntityBase:: |
public | function | ||
EntityBase:: |
protected | function | Gets the entity type bundle info service. | |
EntityBase:: |
protected | function | Gets the entity type manager. | |
EntityBase:: |
public | function | ||
EntityBase:: |
public | function | ||
EntityBase:: |
public | function | ||
EntityBase:: |
public | function | ||
EntityBase:: |
public | function | ||
EntityBase:: |
public | function | ||
EntityBase:: |
protected | function | The list cache tags to invalidate for this entity. | |
EntityBase:: |
public | function | ||
EntityBase:: |
private | function | Returns the typed data class name for this entity. | |
EntityBase:: |
public | function | ||
EntityBase:: |
public | function | 10 | |
EntityBase:: |
public | function | 1 | |
EntityBase:: |
protected | function | Gets the language manager. | |
EntityBase:: |
protected | function | Gets an array link templates. | |
EntityBase:: |
public static | function | ||
EntityBase:: |
public static | function | ||
EntityBase:: |
public | function | 4 | |
EntityBase:: |
public static | function | 12 | |
EntityBase:: |
public static | function | 3 | |
EntityBase:: |
public static | function | 5 | |
EntityBase:: |
public | function | 1 | |
EntityBase:: |
public | function | ||
EntityBase:: |
public | function | ||
EntityBase:: |
protected | function | Gets an array of placeholders for this entity. | 1 |
EntityBase:: |
public | function | 1 | |
EntityBase:: |
protected | function | Gets the UUID generator. | |
PluginDependencyTrait:: |
protected | function | Calculates and adds dependencies of a specific plugin instance. | 1 |
PluginDependencyTrait:: |
protected | function | Calculates and returns dependencies of a specific plugin instance. | |
PluginDependencyTrait:: |
protected | function | Wraps the module handler. | 1 |
PluginDependencyTrait:: |
protected | function | Wraps the theme handler. | 1 |
RefinableCacheableDependencyTrait:: |
public | function | ||
RefinableCacheableDependencyTrait:: |
public | function | ||
RefinableCacheableDependencyTrait:: |
public | function | ||
RefinableCacheableDependencyTrait:: |
public | function | ||
SynchronizableEntityTrait:: |
protected | property | Whether this entity is being created, updated or deleted through a synchronization process. | |
SynchronizableEntityTrait:: |
public | function | ||
SynchronizableEntityTrait:: |
public | function |