From 67bea129b963291735305c83302c7dfb58e9694c Mon Sep 17 00:00:00 2001 From: "Eirik S. Morland" Date: Mon, 10 Jun 2024 13:29:46 +0200 Subject: [PATCH 1/3] Add a Drupal Gutenberg Context --- src/DrupalGutenbergContext.php | 46 ++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/DrupalGutenbergContext.php diff --git a/src/DrupalGutenbergContext.php b/src/DrupalGutenbergContext.php new file mode 100644 index 0000000..467a5f4 --- /dev/null +++ b/src/DrupalGutenbergContext.php @@ -0,0 +1,46 @@ +getStorage($entity_type); + $definition = \Drupal::entityTypeManager()->getDefinition($entity_type); + $entities = $storage->loadByProperties([$definition->getKey('label') => $title]); + $entity = reset($entities); + if (!$entity instanceof EntityInterface) { + throw new \Exception("Entity with title $title not found"); + } + if (!$entity->hasField($field_name)) { + throw new \Exception("Entity with title $title does not have field $field_name"); + } + /** @var \Drupal\Core\Field\FieldItemListInterface $field */ + $field = $entity->get($field_name); + $format = $this->getTextFormat(); + $content = file_get_contents(DRUPAL_ROOT . '/../tests/files/gutenberg/' . $file); + $current_content = ''; + if (!$field->isEmpty()) { + $value = $field->first()->getValue(); + $current_content = $value['value']; + } + $field->setValue([ + 'value' => sprintf("%s\n%s", $current_content, $content), + 'format' => $format, + ]); + $entity->save(); + } + +} From 60dd64d2e4cea1c643ebca5b92e2bd14e7ad533b Mon Sep 17 00:00:00 2001 From: "Eirik S. Morland" Date: Mon, 10 Jun 2024 13:33:23 +0200 Subject: [PATCH 2/3] Code style --- src/DrupalGutenbergContext.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/DrupalGutenbergContext.php b/src/DrupalGutenbergContext.php index 467a5f4..c567cf2 100644 --- a/src/DrupalGutenbergContext.php +++ b/src/DrupalGutenbergContext.php @@ -5,13 +5,23 @@ use Drupal\Core\Entity\EntityInterface; use Drupal\DrupalExtension\Context\RawDrupalContext; +/** + * Some steps to use with Drupal Gutenberg. + */ class DrupalGutenbergContext extends RawDrupalContext { + /** + * Get the text format to use. + * + * This is to make it possible to override in a subclass. + */ protected function getTextFormat() { - + return 'gutenberg'; } /** + * Step to write in a gutenberg field. + * * @Then I create gutenberg content from file :file in :entity_type with title :title and field name :field_name * @Then I create gutenberg content from file :file in content :title and field name :field_name * @Then I create gutenberg content from file :file in content :title From 363dae29c1b9a783ab95425aa822b0aa02f62cfc Mon Sep 17 00:00:00 2001 From: "Eirik S. Morland" Date: Mon, 10 Jun 2024 13:35:22 +0200 Subject: [PATCH 3/3] Use correct interface --- src/DrupalGutenbergContext.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/DrupalGutenbergContext.php b/src/DrupalGutenbergContext.php index c567cf2..4c1835f 100644 --- a/src/DrupalGutenbergContext.php +++ b/src/DrupalGutenbergContext.php @@ -2,7 +2,7 @@ namespace Frontkom\DrupalBehatDefinitions; -use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\Entity\FieldableEntityInterface; use Drupal\DrupalExtension\Context\RawDrupalContext; /** @@ -31,7 +31,7 @@ public function appendContentFromFile($file, $title, $field_name = 'body', $enti $definition = \Drupal::entityTypeManager()->getDefinition($entity_type); $entities = $storage->loadByProperties([$definition->getKey('label') => $title]); $entity = reset($entities); - if (!$entity instanceof EntityInterface) { + if (!$entity instanceof FieldableEntityInterface) { throw new \Exception("Entity with title $title not found"); } if (!$entity->hasField($field_name)) {