diff --git a/panelizer.install b/panelizer.install index e69de29..b3d9bbc 100644 --- a/panelizer.install +++ b/panelizer.install @@ -0,0 +1 @@ +entityTypeManager() + ->getStorage('panelizer_block_content') + ->save($this); + } + +} diff --git a/src/Entity/Storage/ContentEntityNullStorage.php b/src/Entity/Storage/ContentEntityNullStorage.php new file mode 100644 index 0000000..c98363f --- /dev/null +++ b/src/Entity/Storage/ContentEntityNullStorage.php @@ -0,0 +1,31 @@ +entity = unserialize($entity); + } + else { + $bundle = $this->getDerivativeId(); + $this->entity = new BlockContent([], 'block_content', $bundle); + } + } + + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('plugin.manager.block'), + $container->get('entity.manager'), + $container->get('current_user'), + $container->get('url_generator') + ); + + } + + + protected function getEntity() { + return $this->entity; + } + + public function getConfiguration() { + $configuration = parent::getConfiguration(); + $configuration['entity'] = serialize($this->getEntity()); + return $configuration; + } + + public function blockForm($form, FormStateInterface $form_state) { + $options = $this->entityManager->getViewModeOptionsByBundle('panelizer_block_content', $this->getDerivativeId()); + $form['view_mode'] = array( + '#type' => 'select', + '#options' => $options, + '#title' => $this->t('View mode'), + '#description' => $this->t('Output the block in this view mode.'), + '#default_value' => $this->configuration['view_mode'], + '#access' => (count($options) > 1), + ); + $form['title']['#description'] = $this->t('The title of the block as shown to the user.'); + $entity = $this->getEntity(); + if (!$entity->info->getValue()) { + $entity = NULL; + } + $form['entity'] = [ + '#type' => 'inline_entity_form', + '#entity_type' => 'panelizer_block_content', + '#bundle' => $this->getDerivativeId(), + // If the #default_value is NULL, a new entity will be created. + '#default_value' => $entity, + ]; + return $form; + } + + public function blockSubmit($form, FormStateInterface $form_state) { + parent::blockSubmit($form, $form_state); + $entity = $form['entity']['#entity']; + $this->configuration['label'] = $entity->label(); + $langcode = $form_state->getValue('langcode'); + $this->configuration['langcode'] = $langcode; + foreach ($entity->toArray() as $key => $value) { + if ($form_state->hasValue(['entity', $key])) { + $entity->{$key} = $form_state->getValue(['entity', $key]); + } + } + $this->entity = $entity; + } + +} \ No newline at end of file diff --git a/src/Plugin/Derivative/InlineBlock.php b/src/Plugin/Derivative/InlineBlock.php new file mode 100644 index 0000000..0e752a9 --- /dev/null +++ b/src/Plugin/Derivative/InlineBlock.php @@ -0,0 +1,68 @@ +blockContentTypeStorage = $block_content_type_storage; + $this->translation = $translation; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, $base_plugin_id) { + $entity_manager = $container->get('entity.manager'); + return new static( + $entity_manager->getStorage('block_content_type'), + $container->get('string_translation') + ); + } + + /** + * {@inheritdoc} + */ + public function getDerivativeDefinitions($base_plugin_definition) { + $block_types = $this->blockContentTypeStorage->loadMultiple(); + // Reset the discovered definitions. + $this->derivatives = []; + /** @var $type \Drupal\block_content\Entity\BlockContent */ + foreach ($block_types as $type) { + $this->derivatives[$type->id()] = $base_plugin_definition; + $this->derivatives[$type->id()]['admin_label'] = $this->translation->translate('Inline @type', ['@type' => $type->label()]); + $this->derivatives[$type->id()]['config_dependencies']['content'] = array( + $type->getConfigDependencyName() + ); + } + return $this->derivatives; + } + +} diff --git a/tests/src/Kernel/BlockContentTest.php b/tests/src/Kernel/BlockContentTest.php new file mode 100644 index 0000000..c30cdd6 --- /dev/null +++ b/tests/src/Kernel/BlockContentTest.php @@ -0,0 +1,85 @@ +container->get('entity_type.manager') + ->getStorage('block_content_type') + ->create([ + 'id' => 'test_type', + 'label' => 'Test block type', + ]) + ->save(); + + $field_storage = $this->container->get('entity_type.manager') + ->getStorage('field_storage_config') + ->create([ + 'field_name' => 'body', + 'entity_type' => 'block_content', + 'type' => 'text_with_summary', + ]); + $field_storage->save(); + + $this->container->get('entity_type.manager') + ->getStorage('field_config') + ->create([ + 'field_storage' => $field_storage, + 'bundle' => 'test_type', + ]) + ->save(); + } + + public function testBlockWithField() { + $body = $this->randomMachineName(32); + + $block_content = $this->container->get('entity_type.manager') + ->getStorage('panelizer_block_content') + ->create([ + 'type' => 'test_type', + 'info' => 'A test of a Panelizer block_content entity.', + 'body' => $body, + ]); + + $this->assertTrue($block_content->hasField('body')); + $this->assertEquals($body, $block_content->body->value); + + $block_content->save(); + $this->assertEquals($body, $block_content->body->value); + + // The table should be empty. + $count = $this->container->get('database') + ->select('block_content__body') + ->countQuery() + ->execute() + ->fetchField(); + $this->assertEmpty($count); + } + +}