function BlockContentThemeHooks::preprocessEntityAddList

Implements hook_preprocess_HOOK() for entity_add_list.

Adds query parameters to the add links on the block add page so they are forwarded to the add form, this allows a theme to be set automatically on the block.

Attributes

#[Hook('preprocess_entity_add_list')]

See also

\Drupal\block_content\Controller\BlockContentController::addForm()

File

core/modules/block_content/src/Hook/BlockContentThemeHooks.php, line 29

Class

BlockContentThemeHooks
Theme hook implementations for block_content.

Namespace

Drupal\block_content\Hook

Code

public function preprocessEntityAddList(&$variables) : void {
  $query = $this->requestStack
    ->getCurrentRequest()->query
    ->all();
  if (count($query) === 0) {
    return;
  }
  foreach ($variables['bundles'] as $bundleId => $bundle) {
    if ($bundle['add_link'] instanceof Link) {
      $url = $bundle['add_link']->getUrl();
      if ($url->getRouteName() !== 'entity.block_content.add_form') {
        continue;
      }
      $url->setOption('query', $query);
      $variables['bundles'][$bundleId]['add_link']->setUrl($url);
    }
  }
}

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