/** * {@inheritdoc} */ public function getCancelUrl() { $entity_layout = $this->getEntityLayoutFromRouteMatch(); $target_entity_type = $entity_layout->getTargetEntityType(); $bundle_entity_type = $this->entityLayoutService->getTargetBundleEntityType($entity_layout); return Url::fromRoute("entity_layout.{$target_entity_type}.layout", [$bundle_entity_type => $this->entityLayout->getTargetBundle()]); }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { parent::submitForm($form, $form_state); $target_entity = $form_state->getValue('entity'); $target_bundle = $form_state->getValue('bundle', $target_entity); $this->entity->set('target_entity_type', $target_entity); $this->entity->set('target_bundle', $target_bundle); if ($target_entity !== $target_bundle) { drupal_set_message($this->t('A entity layout for the @entity entity and @bundle bundle has been created.', ['@entity' => $this->entityLayoutService->getTargetEntityLabel($this->entity), '@bundle' => $this->entityLayoutService->getTargetBundleLabel($this->entity)])); } else { drupal_set_message($this->t('A entity layout for the @entity entity has been created.', ['@entity' => $this->entityLayoutService->getTargetEntityLabel($this->entity)])); } $form_state->setRedirectUrl($this->entity->toUrl('collection')); }
/** * Transfer the supplied blocks to the specified content entity. * * @param EntityLayoutInterface $entity_layout * The entity layout the entity layout blocks belongs to. * @param ContentEntityInterface $content_entity * The content entity to transfer blocks to. * @param BlockPluginCollection $block_collection * The blocks to transfer. * * @return EntityLayoutBlockInterface[] */ public function transferBlocks(EntityLayoutInterface $entity_layout, ContentEntityInterface $content_entity, BlockPluginCollection $block_collection) { $block_entities = []; // Create a entity layout block entity from each default block // configuration and create a new UUID. /** @var BlockPluginInterface $block */ foreach ($block_collection as $block) { $configuration = $block->getConfiguration(); $block_entity = EntityLayoutBlock::create(['layout' => $entity_layout->id(), 'config' => $configuration, 'entity_id' => $content_entity, 'entity_type' => $content_entity->getEntityTypeId()]); $block_entities[] = $block_entity; } /** @var ContentEntityInterface $block_entity */ foreach ($block_entities as $block_entity) { $block_entity->save(); } return $block_entities; }
/** * Build the render array for the block library. * * @param EntityLayoutInterface $entity_layout * The entity layout to show allowed blocks for. * @param ContentEntityInterface $content_entity * The content entity the block is being added to. * * @return array * The render array. */ protected function buildBlockLibrary(EntityLayoutInterface $entity_layout, ContentEntityInterface $content_entity = NULL) { $build['filter'] = ['#type' => 'search', '#title' => $this->t('Filter'), '#title_display' => 'invisible', '#size' => 30, '#placeholder' => $this->t('Filter by block name'), '#attributes' => ['class' => ['context-table-filter'], 'data-element' => '.block-add-table', 'title' => $this->t('Enter a part of the block name to filter by.')]]; $headers = [$this->t('Block'), $this->t('Category'), $this->t('Operations')]; $build['blocks'] = ['#type' => 'table', '#header' => $headers, '#rows' => [], '#empty' => $this->t('No blocks available for placement.'), '#attributes' => ['class' => ['block-add-table']]]; $blocks = $this->entityLayoutService->getSystemBlocks(); // Add each block definition to the table. foreach ($blocks as $block_id => $block) { if (!$entity_layout->blockIsAllowed($block_id)) { continue; } $bundle_entity_type = $this->entityLayoutService->getTargetBundleEntityType($entity_layout); // Use different routes depending on what kind of entity were adding // blocks for. if ($content_entity) { $entity_type_id = $content_entity->getEntityTypeId(); $block_add_url = Url::fromRoute("entity_layout.{$entity_type_id}.content.block.add", ['block_id' => $block_id, $entity_type_id => $content_entity->id()]); } else { $entity_type_id = $entity_layout->getTargetEntityType(); $block_add_url = Url::fromRoute("entity_layout.{$entity_type_id}.block.add", ['block_id' => $block_id, $bundle_entity_type => $entity_layout->getTargetBundle()]); } $links = ['add' => ['title' => $this->t('Place block'), 'url' => $block_add_url, 'attributes' => ['class' => ['use-ajax'], 'data-dialog-type' => 'modal', 'data-dialog-options' => Json::encode(['width' => 700])]]]; $build['blocks']['#rows'][] = ['title' => ['data' => ['#type' => 'inline_template', '#template' => '<div class="context-table-filter-text-source">{{ label }}</div>', '#context' => ['label' => $block['admin_label']]]], 'category' => ['data' => $block['category']], 'operations' => ['data' => ['#type' => 'operations', '#links' => $links]]]; } // @todo Create a filter behaviour for the table. //$build['#attached']['library'][] = 'context_ui/admin'; return $build; }
/** * Create a new entity layout block. * * @param EntityLayoutInterface $entity_layout * The entity layout object to create a block for. * @param ContentEntityInterface $content_entity * The content entity to associate the block with. * @param array $configuration * The block configuration. * * @return EntityLayoutBlockInterface|static */ public function createContentBlock(EntityLayoutInterface $entity_layout, ContentEntityInterface $content_entity, array $configuration) { $entity_layout_block = EntityLayoutBlock::create(['layout' => $entity_layout->id(), 'config' => $configuration, 'entity_id' => $content_entity, 'entity_type' => $content_entity->getEntityTypeId()]); return $entity_layout_block; }