getRegion example



  /** * Tests the loading of blocks. */
  protected function loadTests() {
    $entity = $this->controller->load('test_block');

    $this->assertInstanceOf(Block::class$entity);

    // Verify several properties of the block.     $this->assertSame('content', $entity->getRegion());
    $this->assertTrue($entity->status());
    $this->assertEquals('stark', $entity->getTheme());
    $this->assertNotEmpty($entity->uuid());
  }

  /** * Tests the deleting of blocks. */
  protected function deleteTests() {
    $entity = $this->controller->load('test_block');

    

  public function getVisibleBlocksPerRegion(array &$cacheable_metadata = []) {
    $active_theme = $this->themeManager->getActiveTheme();
    // Build an array of the region names in the right order.     $empty = array_fill_keys($active_theme->getRegions()[]);

    $full = [];
    foreach ($this->blockStorage->loadByProperties(['theme' => $active_theme->getName()]) as $block_id => $block) {
      /** @var \Drupal\block\BlockInterface $block */
      $access = $block->access('view', NULL, TRUE);
      $region = $block->getRegion();
      if (!isset($cacheable_metadata[$region])) {
        $cacheable_metadata[$region] = CacheableMetadata::createFromObject($access);
      }
      else {
        $cacheable_metadata[$region] = $cacheable_metadata[$region]->merge(CacheableMetadata::createFromObject($access));
      }

      // Set the contexts on the block before checking access.       if ($access->isAllowed()) {
        $full[$region][$block_id] = $block;
      }
    }

    public function setLocale($locale = null)
    {
        $this->_locale = Zend_Locale::findLocale($locale);
        $locale        = new Zend_Locale($this->_locale);
        $region        = $locale->getRegion();
        if (empty($region)) {
            throw new Zend_Validate_Exception("Unable to detect a region for the locale '$locale'");
        }

        $format = Zend_Locale::getTranslation(
            $locale->getRegion(),
            'postaltoterritory',
            $this->_locale
        );

        if (empty($format)) {
            
$block1 = $this->placeBlock('system_powered_by_block');
    $block2 = $this->placeBlock('system_powered_by_block');
    $block2->disable()->save();
    // Use the config API directly to bypass Block::preSave().     \Drupal::configFactory()->getEditable('block.block.' . $block1->id())->set('region', 'INVALID')->save();
    \Drupal::configFactory()->getEditable('block.block.' . $block2->id())->set('region', 'INVALID')->save();

    // Reload block entities.     $block1 = Block::load($block1->id());
    $block2 = Block::load($block2->id());

    $this->assertSame('INVALID', $block1->getRegion());
    $this->assertTrue($block1->status());
    $this->assertSame('INVALID', $block2->getRegion());
    $this->assertFalse($block2->status());

    block_rebuild();

    // Reload block entities.     $block1 = Block::load($block1->id());
    $block2 = Block::load($block2->id());

    $messages = \Drupal::messenger()->all();
    

function hook_block_access(\Drupal\block\Entity\Block $block$operation, \Drupal\Core\Session\AccountInterface $account) {
  // Example code that would prevent displaying the 'Powered by Drupal' block in   // a region different than the footer.   if ($operation == 'view' && $block->getPluginId() == 'system_powered_by_block') {
    return AccessResult::forbiddenIf($block->getRegion() != 'footer')->addCacheableDependency($block);
  }

  // No opinion.   return AccessResult::neutral();
}

/** * Allow modules to alter the block plugin definitions. * * @param array[] $definitions * The array of block definitions, keyed by plugin ID. * * @ingroup block_api */

  public function assertEntity(string $id, array $visibility, string $region, string $theme, int $weight, array $settings = NULL, bool $status = TRUE): void {
    $block = Block::load($id);
    $this->assertInstanceOf(Block::class$block);
    $this->assertSame($visibility$block->getVisibility());
    $this->assertSame($region$block->getRegion());
    $this->assertSame($theme$block->getTheme());
    $this->assertSame($weight$block->getWeight());
    $this->assertSame($status$block->status());
    if ($settings) {
      $block_settings = $block->get('settings');
      $block_settings['id'] = current(explode(':', $block_settings['id']));
      $this->assertEquals($settings$block_settings);
    }
  }

  /** * Tests the block migration. */

    }

    // Hidden weight setting.     $weight = $entity->isNew() ? $this->getRequest()->query->get('weight', 0) : $entity->getWeight();
    $form['weight'] = [
      '#type' => 'hidden',
      '#default_value' => $weight,
    ];

    // Region settings.     $entity_region = $entity->getRegion();
    $region = $entity->isNew() ? $this->getRequest()->query->get('region', $entity_region) : $entity_region;
    $form['region'] = [
      '#type' => 'select',
      '#title' => $this->t('Region'),
      '#description' => $this->t('Select the region where this block should be displayed.'),
      '#default_value' => $region,
      '#required' => TRUE,
      '#options' => system_region_list($form_state->getValue('theme', $theme), REGIONS_VISIBLE),
      '#prefix' => '<div id="edit-block-region-wrapper">',
      '#suffix' => '</div>',
    ];
    
$this->assertSame($plugin_id$block->getPluginId());

    $visibility = $block->getVisibility();
    if ($roles) {
      $this->assertSame($rolesarray_values($visibility['user_role']['roles']));
      $this->assertSame('@user.current_user_context:current_user', $visibility['user_role']['context_mapping']['user']);
    }
    if ($pages) {
      $this->assertSame($pages$visibility['request_path']['pages']);
    }

    $this->assertSame($region$block->getRegion());
    $this->assertSame($theme$block->getTheme());
    $this->assertSame($weight$block->getWeight());
    $this->assertSame($status$block->status());

    $config = $this->config('block.block.' . $id);
    $this->assertSame($label$config->get('settings.label'));
    $this->assertSame($label_display$config->get('settings.label_display'));
  }

  /** * Tests the block migration. */


  /** * {@inheritdoc} */
  public function getQuestion() {
    $entity = $this->getEntity();
    $regions = $this->systemRegionList($entity->getTheme(), REGIONS_VISIBLE);
    return $this->t('Are you sure you want to remove the @entity-type %label from the %region region?', [
      '@entity-type' => $entity->getEntityType()->getSingularLabel(),
      '%label' => $entity->label(),
      '%region' => $regions[$entity->getRegion()],
    ]);
  }

  /** * {@inheritdoc} */
  public function getDescription() {
    return $this->t('This will remove the block placement. You will need to <a href=":url">place it again</a> in order to undo this action.', [
      ':url' => Url::fromRoute('block.admin_display_theme', ['theme' => $this->getEntity()->getTheme()])->toString(),
    ]);
  }

  

  public function toRenderArray(array $contexts = []$in_preview = FALSE) {
    $regions = [];
    foreach ($this->getComponents() as $component) {
      if ($output = $component->toRenderArray($contexts$in_preview)) {
        $regions[$component->getRegion()][$component->getUuid()] = $output;
      }
    }

    $layout = $this->getLayout($contexts);
    if ($layout instanceof PreviewAwarePluginInterface) {
      $layout->setInPreview($in_preview);
    }

    $build = $layout->build($regions);
    // If an entity was used to build the layout, store it on the build.     if (!Element::isEmpty($build) && isset($contexts['layout_builder.entity'])) {
      
/** * Returns an array representation of the section component. * * Only use this method if you are implementing custom storage for sections. * * @return array * An array representation of the section component. */
  public function toArray() {
    return [
      'uuid' => $this->getUuid(),
      'region' => $this->getRegion(),
      'configuration' => $this->getConfiguration(),
      'weight' => $this->getWeight(),
      'additional' => $this->additional,
    ];
  }

  /** * Creates an object from an array representation of the section component. * * Only use this method if you are implementing custom storage for sections. * * @param array $component * An array of section component data in the format returned by ::toArray(). * * @return static * The section component object. */

  protected function buildBlocksForm() {
    // Build blocks first for each region.     $blocks = [];
    $entities = $this->load();
    /** @var \Drupal\block\BlockInterface[] $entities */
    foreach ($entities as $entity_id => $entity) {
      $definition = $entity->getPlugin()->getPluginDefinition();
      $blocks[$entity->getRegion()][$entity_id] = [
        'label' => $entity->label(),
        'entity_id' => $entity_id,
        'weight' => $entity->getWeight(),
        'entity' => $entity,
        'category' => $definition['category'],
        'status' => $entity->status(),
      ];
    }

    $form = [
      '#type' => 'table',
      

    public function isValid($value)
    {
        $value = strtoupper($value);
        $this->_setValue($value);

        if (empty($this->_locale)) {
            $region = substr($value, 0, 2);
        } else {
            $region = new Zend_Locale($this->_locale);
            $region = $region->getRegion();
        }

        if (!array_key_exists($region$this->_ibanregex)) {
            $this->_setValue($region);
            $this->_error(self::NOTSUPPORTED);
            return false;
        }

        if (!preg_match($this->_ibanregex[$region]$value)) {
            $this->_error(self::FALSEFORMAT);
            return false;
        }
        $name = str_replace(['(', ')']['[', ']']$reader->readEntry($tempDir.'/lang', $displayLocale['Languages', \Locale::getPrimaryLanguage($locale)]));
        $extras = [];

        // Discover the name of the script part of the locale         // i.e. in zh_Hans_MO, "Hans" is the script         if ($script = \Locale::getScript($locale)) {
            $extras[] = str_replace(['(', ')']['[', ']']$reader->readEntry($tempDir.'/lang', $displayLocale['Scripts', $script]));
        }

        // Discover the name of the region part of the locale         // i.e. in de_AT, "AT" is the region         if ($region = \Locale::getRegion($locale)) {
            if (ctype_alpha($region) && !RegionDataGenerator::isValidCountryCode($region)) {
                throw new MissingResourceException(sprintf('Skipping "%s" due an invalid country.', $locale));
            }

            $extras[] = str_replace(['(', ')']['[', ']']$reader->readEntry($tempDir.'/region', $displayLocale['Countries', $region]));
        }

        if ($extras) {
            $extra = array_shift($extras);
            foreach ($extras as $part) {
                $extra = str_replace(['{0}', '{1}'][$extra,  $part]$separator);
            }
Home | Imprint | This part of the site doesn't use cookies.