Chris@0: get('entity.manager'); Chris@0: return new static( Chris@0: $entity_manager->getStorage('block_content'), Chris@0: $entity_manager->getStorage('block_content_type'), Chris@0: $container->get('theme_handler') Chris@0: ); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Constructs a BlockContent object. Chris@0: * Chris@0: * @param \Drupal\Core\Entity\EntityStorageInterface $block_content_storage Chris@0: * The custom block storage. Chris@0: * @param \Drupal\Core\Entity\EntityStorageInterface $block_content_type_storage Chris@0: * The custom block type storage. Chris@0: * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler Chris@0: * The theme handler. Chris@0: */ Chris@0: public function __construct(EntityStorageInterface $block_content_storage, EntityStorageInterface $block_content_type_storage, ThemeHandlerInterface $theme_handler) { Chris@0: $this->blockContentStorage = $block_content_storage; Chris@0: $this->blockContentTypeStorage = $block_content_type_storage; Chris@0: $this->themeHandler = $theme_handler; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Displays add custom block links for available types. Chris@0: * Chris@0: * @param \Symfony\Component\HttpFoundation\Request $request Chris@0: * The current request object. Chris@0: * Chris@0: * @return array Chris@0: * A render array for a list of the custom block types that can be added or Chris@0: * if there is only one custom block type defined for the site, the function Chris@0: * returns the custom block add page for that custom block type. Chris@0: */ Chris@0: public function add(Request $request) { Chris@0: $types = $this->blockContentTypeStorage->loadMultiple(); Chris@0: if ($types && count($types) == 1) { Chris@0: $type = reset($types); Chris@0: return $this->addForm($type, $request); Chris@0: } Chris@0: if (count($types) === 0) { Chris@0: return [ Chris@0: '#markup' => $this->t('You have not created any block types yet. Go to the block type creation page to add a new block type.', [ Chris@0: ':url' => Url::fromRoute('block_content.type_add')->toString(), Chris@0: ]), Chris@0: ]; Chris@0: } Chris@0: Chris@0: return ['#theme' => 'block_content_add_list', '#content' => $types]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Presents the custom block creation form. Chris@0: * Chris@0: * @param \Drupal\block_content\BlockContentTypeInterface $block_content_type Chris@0: * The custom block type to add. Chris@0: * @param \Symfony\Component\HttpFoundation\Request $request Chris@0: * The current request object. Chris@0: * Chris@0: * @return array Chris@16: * A form array as expected by Chris@16: * \Drupal\Core\Render\RendererInterface::render(). Chris@0: */ Chris@0: public function addForm(BlockContentTypeInterface $block_content_type, Request $request) { Chris@0: $block = $this->blockContentStorage->create([ Chris@17: 'type' => $block_content_type->id(), Chris@0: ]); Chris@0: if (($theme = $request->query->get('theme')) && in_array($theme, array_keys($this->themeHandler->listInfo()))) { Chris@0: // We have navigated to this page from the block library and will keep track Chris@0: // of the theme for redirecting the user to the configuration page for the Chris@0: // newly created block in the given theme. Chris@0: $block->setTheme($theme); Chris@0: } Chris@0: return $this->entityFormBuilder()->getForm($block); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Provides the page title for this controller. Chris@0: * Chris@0: * @param \Drupal\block_content\BlockContentTypeInterface $block_content_type Chris@0: * The custom block type being added. Chris@0: * Chris@0: * @return string Chris@0: * The page title. Chris@0: */ Chris@0: public function getAddFormTitle(BlockContentTypeInterface $block_content_type) { Chris@0: return $this->t('Add %type custom block', ['%type' => $block_content_type->label()]); Chris@0: } Chris@0: Chris@0: }