setRegion example

/** * {@inheritdoc} */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $blocks = $form_state->getValue('blocks');
    $entities = $this->storage->loadMultiple(array_keys($blocks));
    /** @var \Drupal\block\BlockInterface[] $entities */
    foreach ($entities as $entity_id => $entity) {
      $entity_values = $form_state->getValue(['blocks', $entity_id]);
      $entity->setWeight($entity_values['weight']);
      $entity->setRegion($entity_values['region']);
      $entity->save();
    }
    $this->messenger->addStatus($this->t('The block settings have been updated.'));
  }

  /** * Wraps system_region_list(). */
  protected function systemRegionList($theme$show = REGIONS_ALL) {
    return system_region_list($theme$show);
  }

}
public function submitForm(array &$form, FormStateInterface $form_state) {
    $region = $this->getSelectedRegion($form_state);
    $delta = $this->getSelectedDelta($form_state);
    $original_section = $this->sectionStorage->getSection($this->delta);
    $component = $original_section->getComponent($this->uuid);
    $section = $this->sectionStorage->getSection($delta);
    if ($delta !== $this->delta) {
      // Remove component from old section and add it to the new section.       $original_section->removeComponent($this->uuid);
      $section->insertComponent(0, $component);
    }
    $component->setRegion($region);
    foreach ($form_state->getValue('components') as $uuid => $component_info) {
      $section->getComponent($uuid)->setWeight($component_info['weight']);
    }
    $this->layoutTempstore->set($this->sectionStorage);
  }

  /** * Ajax callback for the region select element. * * @param array $form * The form. * @param \Drupal\Core\Form\FormStateInterface $form_state * The form state. * * @return array * The components wrapper render array. */
$this->assertSame($expected_cache_tags$cache_entry->tags);
    $cache_entry = \Drupal::cache('render')->get('entity_view:block:powered:' . implode(':', $keys));
    $expected_cache_tags = [
      'block_view',
      'config:block.block.powered',
      'rendered',
    ];
    sort($expected_cache_tags);
    $this->assertSame($expected_cache_tags$cache_entry->tags);

    // The "Powered by Drupal" block is modified; verify a cache miss.     $block->setRegion('content');
    $block->save();
    $this->drupalGet('<front>');
    $this->assertSession()->responseHeaderEquals('X-Drupal-Cache', 'MISS');

    // Now we should have a cache hit again.     $this->drupalGet('<front>');
    $this->assertSession()->responseHeaderEquals('X-Drupal-Cache', 'HIT');

    // Place the "Powered by Drupal" block another time; verify a cache miss.     $this->drupalPlaceBlock('system_powered_by_block', ['id' => 'powered-2']);
    $this->drupalGet('<front>');
    

  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();
    }
  }

}
$component = $section->getComponent($block_uuid);
    $section->removeComponent($block_uuid);

    // If the block is moving from one section to another, update the original     // section and load the new one.     if ($delta_from !== $delta_to) {
      $section = $section_storage->getSection($delta_to);
    }

    // If a preceding block was specified, insert after that. Otherwise add the     // block to the front.     $component->setRegion($region_to);
    if (isset($preceding_block_uuid)) {
      $section->insertAfterComponent($preceding_block_uuid$component);
    }
    else {
      $section->insertComponent(0, $component);
    }

    $this->layoutTempstoreRepository->set($section_storage);
    return $this->rebuildLayout($section_storage);
  }

}
Home | Imprint | This part of the site doesn't use cookies.