class Block

Same name in this branch
  1. 10 core/modules/views/src/Plugin/views/display/Block.php \Drupal\views\Plugin\views\display\Block
  2. 10 core/modules/block/src/Plugin/migrate/source/Block.php \Drupal\block\Plugin\migrate\source\Block
  3. 10 core/lib/Drupal/Core/Block/Attribute/Block.php \Drupal\Core\Block\Attribute\Block
  4. 10 core/lib/Drupal/Core/Block/Annotation/Block.php \Drupal\Core\Block\Annotation\Block
Same name in other branches
  1. 9 core/modules/views/src/Plugin/views/display/Block.php \Drupal\views\Plugin\views\display\Block
  2. 9 core/modules/block/src/Entity/Block.php \Drupal\block\Entity\Block
  3. 9 core/modules/block/src/Plugin/migrate/source/Block.php \Drupal\block\Plugin\migrate\source\Block
  4. 9 core/lib/Drupal/Core/Block/Annotation/Block.php \Drupal\Core\Block\Annotation\Block
  5. 8.9.x core/modules/views/src/Plugin/views/display/Block.php \Drupal\views\Plugin\views\display\Block
  6. 8.9.x core/modules/block/src/Entity/Block.php \Drupal\block\Entity\Block
  7. 8.9.x core/modules/block/src/Plugin/migrate/source/Block.php \Drupal\block\Plugin\migrate\source\Block
  8. 8.9.x core/lib/Drupal/Core/Block/Annotation/Block.php \Drupal\Core\Block\Annotation\Block
  9. 11.x core/modules/views/src/Plugin/views/display/Block.php \Drupal\views\Plugin\views\display\Block
  10. 11.x core/modules/block/src/Entity/Block.php \Drupal\block\Entity\Block
  11. 11.x core/modules/block/src/Plugin/migrate/source/Block.php \Drupal\block\Plugin\migrate\source\Block
  12. 11.x core/lib/Drupal/Core/Block/Attribute/Block.php \Drupal\Core\Block\Attribute\Block
  13. 11.x core/lib/Drupal/Core/Block/Annotation/Block.php \Drupal\Core\Block\Annotation\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

Expanded class hierarchy of Block

46 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
BigPipeInterfacePreviewThemeSuggestionsTest.php in core/modules/big_pipe/tests/src/Kernel/BigPipeInterfacePreviewThemeSuggestionsTest.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

... See full list

207 string references to 'Block'
AjaxFormCacheTest::testBlockForms in core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxFormCacheTest.php
Tests AJAX forms in blocks.
AjaxFormCacheTest::testQueryString in core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxFormCacheTest.php
Tests AJAX forms on pages with a query string.
AreaDisplayLinkTest::setUp in core/modules/views/tests/src/Kernel/Handler/AreaDisplayLinkTest.php
BigPipeInterfacePreviewThemeSuggestionsTest::setUp in core/modules/big_pipe/tests/src/Kernel/BigPipeInterfacePreviewThemeSuggestionsTest.php
big_pipe_theme_suggestions_big_pipe_interface_preview in core/modules/big_pipe/big_pipe.module
Implements hook_theme_suggestions_HOOK().

... See full list

File

core/modules/block/src/Entity/Block.php, line 64

Namespace

Drupal\block\Entity
View 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

Title Sort descending Modifiers Object type Summary Member alias Overriden Title Overrides
Block::$conditionPluginManager protected property The condition plugin manager.
Block::$contexts protected property The available contexts for this block and its visibility conditions.
Block::$id protected property The ID of the block.
Block::$plugin protected property The plugin instance ID.
Block::$pluginCollection protected property The plugin collection that holds the block plugin for this entity.
Block::$region protected property The region this block is placed in.
Block::$settings protected property The plugin instance settings.
Block::$theme protected property The theme that includes the block plugin for this entity.
Block::$visibility protected property The visibility settings for this block.
Block::$visibilityCollection protected property The visibility collection.
Block::$weight protected property The block weight.
Block::calculateDependencies public function Overrides ConfigEntityBase::calculateDependencies
Block::conditionPluginManager protected function Gets the condition plugin manager.
Block::createDuplicateBlock public function Overrides BlockInterface::createDuplicateBlock
Block::getPlugin public function Overrides BlockInterface::getPlugin
Block::getPluginCollection protected function Encapsulates the creation of the block's LazyPluginCollection.
Block::getPluginCollections public function
Block::getPluginId public function Overrides BlockInterface::getPluginId
Block::getRegion public function Overrides BlockInterface::getRegion
Block::getTheme public function Overrides BlockInterface::getTheme
Block::getVisibility public function Overrides BlockInterface::getVisibility
Block::getVisibilityCondition public function Overrides BlockInterface::getVisibilityCondition
Block::getVisibilityConditions public function Overrides BlockInterface::getVisibilityConditions
Block::getWeight public function Overrides BlockInterface::getWeight
Block::label public function Overrides EntityBase::label
Block::postSave public function Overrides EntityBase::postSave
Block::preSave public function Overrides ConfigEntityBase::preSave
Block::setRegion public function Overrides BlockInterface::setRegion
Block::setVisibilityConfig public function Overrides BlockInterface::setVisibilityConfig
Block::setWeight public function Overrides BlockInterface::setWeight
Block::sort public static function Sorts active blocks by weight; sorts inactive blocks by name. Overrides ConfigEntityBase::sort
CacheableDependencyTrait::$cacheContexts protected property Cache contexts.
CacheableDependencyTrait::$cacheMaxAge protected property Cache max-age.
CacheableDependencyTrait::$cacheTags protected property Cache tags.
CacheableDependencyTrait::setCacheability protected function Sets cacheability; useful for value object constructors.
ConfigEntityBase::$isUninstalling private property Whether the config is being deleted by the uninstall process.
ConfigEntityBase::$langcode protected property The language code of the entity's default language.
ConfigEntityBase::$originalId protected property The original ID of the configuration entity.
ConfigEntityBase::$status protected property The enabled/disabled status of the configuration entity. 4
ConfigEntityBase::$third_party_settings protected property
ConfigEntityBase::$trustedData protected property Trust supplied data and not use configuration schema on save.
ConfigEntityBase::$uuid protected property The UUID for this entity.
ConfigEntityBase::$_core protected property
ConfigEntityBase::addDependency protected function Overrides \Drupal\Core\Entity\DependencyTrait:addDependency().
ConfigEntityBase::createDuplicate public function Overrides EntityBase::createDuplicate 1
ConfigEntityBase::disable public function Overrides ConfigEntityInterface::disable 1
ConfigEntityBase::enable public function Overrides ConfigEntityInterface::enable
ConfigEntityBase::get public function Overrides ConfigEntityInterface::get
ConfigEntityBase::getCacheTagsToInvalidate public function Overrides EntityBase::getCacheTagsToInvalidate 1
ConfigEntityBase::getConfigDependencyName public function Overrides EntityBase::getConfigDependencyName
ConfigEntityBase::getConfigManager protected static function Gets the configuration manager.
ConfigEntityBase::getConfigTarget public function Overrides EntityBase::getConfigTarget
ConfigEntityBase::getDependencies public function Overrides ConfigEntityInterface::getDependencies
ConfigEntityBase::getOriginalId public function Overrides EntityBase::getOriginalId
ConfigEntityBase::getThirdPartyProviders public function Overrides ThirdPartySettingsInterface::getThirdPartyProviders
ConfigEntityBase::getThirdPartySetting public function Overrides ThirdPartySettingsInterface::getThirdPartySetting
ConfigEntityBase::getThirdPartySettings public function Overrides ThirdPartySettingsInterface::getThirdPartySettings
ConfigEntityBase::getTypedConfig protected function Gets the typed config manager.
ConfigEntityBase::hasTrustedData public function Overrides ConfigEntityInterface::hasTrustedData
ConfigEntityBase::invalidateTagsOnDelete protected static function Override to never invalidate the individual entities' cache tags; the
config system already invalidates them.
Overrides EntityBase::invalidateTagsOnDelete
ConfigEntityBase::invalidateTagsOnSave protected function Override to never invalidate the entity's cache tag; the config system
already invalidates it.
Overrides EntityBase::invalidateTagsOnSave
ConfigEntityBase::isInstallable public function Overrides ConfigEntityInterface::isInstallable 2
ConfigEntityBase::isNew public function Overrides Entity::isNew(). Overrides EntityBase::isNew
ConfigEntityBase::isUninstalling public function Overrides ConfigEntityInterface::isUninstalling
ConfigEntityBase::onDependencyRemoval public function Overrides ConfigEntityInterface::onDependencyRemoval 8
ConfigEntityBase::preDelete public static function Overrides EntityBase::preDelete 8
ConfigEntityBase::save public function Overrides EntityBase::save 2
ConfigEntityBase::set public function Overrides ConfigEntityInterface::set
ConfigEntityBase::setOriginalId public function Overrides EntityBase::setOriginalId
ConfigEntityBase::setStatus public function Overrides ConfigEntityInterface::setStatus
ConfigEntityBase::setThirdPartySetting public function Overrides ThirdPartySettingsInterface::setThirdPartySetting
ConfigEntityBase::setUninstalling public function
ConfigEntityBase::status public function Overrides ConfigEntityInterface::status 4
ConfigEntityBase::toArray public function Overrides EntityBase::toArray 3
ConfigEntityBase::toUrl public function Overrides EntityBase::toUrl
ConfigEntityBase::trustData public function Overrides ConfigEntityInterface::trustData
ConfigEntityBase::unsetThirdPartySetting public function Overrides ThirdPartySettingsInterface::unsetThirdPartySetting
ConfigEntityBase::__construct public function Overrides EntityBase::__construct 10
ConfigEntityBase::__sleep public function Overrides EntityBase::__sleep 4
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function Aliased as: traitSleep 1
DependencySerializationTrait::__wakeup public function 2
DependencyTrait::$dependencies protected property The object's dependencies.
DependencyTrait::addDependencies protected function Adds multiple dependencies.
DependencyTrait::addDependency protected function Adds a dependency. Aliased as: addDependencyTrait
EntityBase::$enforceIsNew protected property Boolean indicating whether the entity should be forced to be new.
EntityBase::$entityTypeId protected property The entity type.
EntityBase::$typedData protected property A typed data object wrapping this entity.
EntityBase::access public function Overrides AccessibleInterface::access 1
EntityBase::bundle public function Overrides EntityInterface::bundle 1
EntityBase::create public static function Overrides EntityInterface::create 2
EntityBase::delete public function Overrides EntityInterface::delete 1
EntityBase::enforceIsNew public function Overrides EntityInterface::enforceIsNew
EntityBase::entityTypeBundleInfo protected function Gets the entity type bundle info service.
EntityBase::entityTypeManager protected function Gets the entity type manager.
EntityBase::getCacheContexts public function Overrides CacheableDependencyTrait::getCacheContexts
EntityBase::getCacheMaxAge public function Overrides CacheableDependencyTrait::getCacheMaxAge
EntityBase::getCacheTags public function Overrides CacheableDependencyTrait::getCacheTags
EntityBase::getConfigDependencyKey public function Overrides EntityInterface::getConfigDependencyKey
EntityBase::getEntityType public function Overrides EntityInterface::getEntityType
EntityBase::getEntityTypeId public function Overrides EntityInterface::getEntityTypeId
EntityBase::getListCacheTagsToInvalidate protected function The list cache tags to invalidate for this entity.
EntityBase::getTypedData public function Overrides EntityInterface::getTypedData
EntityBase::getTypedDataClass private function Returns the typed data class name for this entity.
EntityBase::hasLinkTemplate public function Overrides EntityInterface::hasLinkTemplate
EntityBase::id public function Overrides EntityInterface::id 10
EntityBase::language public function Overrides EntityInterface::language 1
EntityBase::languageManager protected function Gets the language manager.
EntityBase::linkTemplates protected function Gets an array link templates. 1
EntityBase::load public static function Overrides EntityInterface::load
EntityBase::loadMultiple public static function Overrides EntityInterface::loadMultiple
EntityBase::postCreate public function Overrides EntityInterface::postCreate 5
EntityBase::postDelete public static function Overrides EntityInterface::postDelete 17
EntityBase::postLoad public static function Overrides EntityInterface::postLoad 3
EntityBase::preCreate public static function Overrides EntityInterface::preCreate 6
EntityBase::referencedEntities public function Overrides EntityInterface::referencedEntities 1
EntityBase::toLink public function Overrides EntityInterface::toLink
EntityBase::uriRelationships public function Overrides EntityInterface::uriRelationships
EntityBase::urlRouteParameters protected function Gets an array of placeholders for this entity. 2
EntityBase::uuid public function Overrides EntityInterface::uuid 1
EntityBase::uuidGenerator protected function Gets the UUID generator.
PluginDependencyTrait::calculatePluginDependencies protected function Calculates and adds dependencies of a specific plugin instance. 1
PluginDependencyTrait::getPluginDependencies protected function Calculates and returns dependencies of a specific plugin instance.
PluginDependencyTrait::moduleHandler protected function Wraps the module handler. 1
PluginDependencyTrait::themeHandler protected function Wraps the theme handler. 1
RefinableCacheableDependencyTrait::addCacheableDependency public function 1
RefinableCacheableDependencyTrait::addCacheContexts public function
RefinableCacheableDependencyTrait::addCacheTags public function
RefinableCacheableDependencyTrait::mergeCacheMaxAge public function
SynchronizableEntityTrait::$isSyncing protected property Is entity being created updated or deleted through synchronization process.
SynchronizableEntityTrait::isSyncing public function
SynchronizableEntityTrait::setSyncing public function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.