diff --git a/html/modules/custom/ghi_content/src/Entity/ContentBase.php b/html/modules/custom/ghi_content/src/Entity/ContentBase.php index ce852b012..a95fa7592 100644 --- a/html/modules/custom/ghi_content/src/Entity/ContentBase.php +++ b/html/modules/custom/ghi_content/src/Entity/ContentBase.php @@ -257,6 +257,9 @@ public function getStructuralTags() { * The content space term. */ public function getContentSpace() { + if (!$this->hasField('field_content_space')) { + return NULL; + } return $this->get('field_content_space')?->entity ?? NULL; } diff --git a/html/modules/custom/ghi_content/tests/src/Kernel/ArticleManagerTest.php b/html/modules/custom/ghi_content/tests/src/Kernel/ArticleManagerTest.php index 944deabb2..893b4f4c6 100644 --- a/html/modules/custom/ghi_content/tests/src/Kernel/ArticleManagerTest.php +++ b/html/modules/custom/ghi_content/tests/src/Kernel/ArticleManagerTest.php @@ -3,14 +3,13 @@ namespace Drupal\Tests\ghi_content\Kernel; use Drupal\Core\Field\FieldStorageDefinitionInterface; -use Drupal\field\Entity\FieldConfig; -use Drupal\field\Entity\FieldStorageConfig; use Drupal\ghi_content\Entity\Article; use Drupal\ghi_sections\Entity\Section; use Drupal\KernelTests\KernelTestBase; use Drupal\node\Entity\Node; use Drupal\node\Entity\NodeType; use Drupal\node\NodeInterface; +use Drupal\Tests\field\Traits\EntityReferenceFieldCreationTrait; use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait; use Drupal\Tests\user\Traits\UserCreationTrait; @@ -23,6 +22,7 @@ class ArticleManagerTest extends KernelTestBase { use TaxonomyTestTrait; use UserCreationTrait; + use EntityReferenceFieldCreationTrait; /** * Modules to enable. @@ -89,43 +89,13 @@ protected function setUp(): void { $this->vocabulary = $this->createVocabulary(); // Setup the tags field on our node types. - $field_storage = FieldStorageConfig::create([ - 'field_name' => 'field_tags', - 'entity_type' => 'node', - 'type' => 'entity_reference', - 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED, - 'settings' => [ - 'target_type' => 'taxonomy_term', + $handler_settings = [ + 'target_bundles' => [ + $this->vocabulary->id() => $this->vocabulary->id(), ], - ]); - $field_storage->save(); - - FieldConfig::create([ - 'field_name' => 'field_tags', - 'field_storage' => $field_storage, - 'bundle' => self::SECTION_BUNDLE, - 'settings' => [ - 'handler' => 'default', - 'handler_settings' => [ - 'target_bundles' => [ - $this->vocabulary->id() => $this->vocabulary->id(), - ], - ], - ], - ])->save(); - FieldConfig::create([ - 'field_name' => 'field_tags', - 'field_storage' => $field_storage, - 'bundle' => self::ARTICLE_BUNDLE, - 'settings' => [ - 'handler' => 'default', - 'handler_settings' => [ - 'target_bundles' => [ - $this->vocabulary->id() => $this->vocabulary->id(), - ], - ], - ], - ])->save(); + ]; + $this->createEntityReferenceField('node', self::SECTION_BUNDLE, 'field_tags', 'Tags', 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED); + $this->createEntityReferenceField('node', self::ARTICLE_BUNDLE, 'field_tags', 'Tags', 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED); // $this->setUpCurrentUser([], ['access content']); $this->setUpCurrentUser(['uid' => 1]); diff --git a/html/modules/custom/ghi_content/tests/src/Kernel/DocumentManagerTest.php b/html/modules/custom/ghi_content/tests/src/Kernel/DocumentManagerTest.php index 6992fb0ef..33078ef5c 100644 --- a/html/modules/custom/ghi_content/tests/src/Kernel/DocumentManagerTest.php +++ b/html/modules/custom/ghi_content/tests/src/Kernel/DocumentManagerTest.php @@ -3,13 +3,12 @@ namespace Drupal\Tests\ghi_content\Kernel; use Drupal\Core\Field\FieldStorageDefinitionInterface; -use Drupal\field\Entity\FieldConfig; -use Drupal\field\Entity\FieldStorageConfig; use Drupal\ghi_sections\Entity\Section; use Drupal\KernelTests\KernelTestBase; use Drupal\node\Entity\Node; use Drupal\node\Entity\NodeType; use Drupal\taxonomy\TermInterface; +use Drupal\Tests\field\Traits\EntityReferenceFieldCreationTrait; use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait; use Drupal\Tests\user\Traits\UserCreationTrait; @@ -22,6 +21,7 @@ class DocumentManagerTest extends KernelTestBase { use TaxonomyTestTrait; use UserCreationTrait; + use EntityReferenceFieldCreationTrait; /** * Modules to enable. @@ -52,7 +52,7 @@ class DocumentManagerTest extends KernelTestBase { const DOCUMENT_BUNDLE = 'document'; /** - * A vocabulary. + * A vocabulary for tags. * * @var \Drupal\taxonomy\VocabularyInterface */ @@ -87,45 +87,14 @@ protected function setUp(): void { $this->vocabulary = $this->createVocabulary(); // Setup the tags field on our node types. - $field_storage = FieldStorageConfig::create([ - 'field_name' => 'field_tags', - 'entity_type' => 'node', - 'type' => 'entity_reference', - 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED, - 'settings' => [ - 'target_type' => 'taxonomy_term', + $handler_settings = [ + 'target_bundles' => [ + $this->vocabulary->id() => $this->vocabulary->id(), ], - ]); - $field_storage->save(); - - FieldConfig::create([ - 'field_name' => 'field_tags', - 'field_storage' => $field_storage, - 'bundle' => self::SECTION_BUNDLE, - 'settings' => [ - 'handler' => 'default', - 'handler_settings' => [ - 'target_bundles' => [ - $this->vocabulary->id() => $this->vocabulary->id(), - ], - ], - ], - ])->save(); - FieldConfig::create([ - 'field_name' => 'field_tags', - 'field_storage' => $field_storage, - 'bundle' => self::DOCUMENT_BUNDLE, - 'settings' => [ - 'handler' => 'default', - 'handler_settings' => [ - 'target_bundles' => [ - $this->vocabulary->id() => $this->vocabulary->id(), - ], - ], - ], - ])->save(); + ]; + $this->createEntityReferenceField('node', self::SECTION_BUNDLE, 'field_tags', 'Tags', 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED); + $this->createEntityReferenceField('node', self::DOCUMENT_BUNDLE, 'field_tags', 'Tags', 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED); - // $this->setUpCurrentUser([], ['access content']); $this->setUpCurrentUser(['uid' => 1]); }