function Block::preSave

Same name and namespace in other branches
  1. 11.x core/modules/block/src/Entity/Block.php \Drupal\block\Entity\Block::preSave()
  2. 10 core/modules/block/src/Entity/Block.php \Drupal\block\Entity\Block::preSave()
  3. 9 core/modules/block/src/Entity/Block.php \Drupal\block\Entity\Block::preSave()
  4. 8.9.x core/modules/block/src/Entity/Block.php \Drupal\block\Entity\Block::preSave()

Overrides ConfigEntityBase::preSave

File

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

Class

Block
Defines a Block configuration entity class.

Namespace

Drupal\block\Entity

Code

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.
  try {
    $theme_extension = \Drupal::service(ThemeHandlerInterface::class)->getTheme($this->theme);
    $regions = $theme_extension->listAllRegions();
    $default_region = $theme_extension->getDefaultRegion();
  } catch (UnknownExtensionException) {
    $regions = [];
    $default_region = '';
  }
  if (!isset($this->region, $regions[$this->region]) && $this->status()) {
    $this->setRegion($default_region)
      ->disable();
  }
}

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