diff --git a/config/schema/eck.schema.yml b/config/schema/eck.schema.yml index 4a30f06..1023cd8 100644 --- a/config/schema/eck.schema.yml +++ b/config/schema/eck.schema.yml @@ -32,6 +32,9 @@ eck.eck_entity_type.*: title: type: boolean label: 'Title' + status: + type: boolean + label: 'Status' eck.eck_type.*: type: config_entity diff --git a/eck.install b/eck.install index e3171a2..8e564dc 100644 --- a/eck.install +++ b/eck.install @@ -82,3 +82,30 @@ function eck_update_8004() { $entityDefinitionUpdateManager->installEntityType($entityTypeDefinition); } } + +/** + * Add 'status' key for entity type config. + */ +function eck_update_8005() { + $config_factory = \Drupal::configFactory(); + foreach ($config_factory->listAll('eck.eck_entity_type.') as $name) { + $config = $config_factory->getEditable($name); + // By default 'status' field will be disabled for existing entities types. + $config->set('status', FALSE); + $config->save(); + } +} + +/** + * Set the 'published' entity key. + */ +function eck_update_8006() { + $definition_update_manager = \Drupal::entityDefinitionUpdateManager(); + foreach (\Drupal::entityTypeManager()->getStorage('eck_entity_type')->loadMultiple() as $entity_type) { + $entity_type = $definition_update_manager->getEntityType($entity_type->id()); + $keys = $entity_type->getKeys(); + $keys['published'] = 'status'; + $entity_type->set('entity_keys', $keys); + $definition_update_manager->updateEntityType($entity_type); + } +} diff --git a/eck.module b/eck.module index 1b13bf6..726659d 100644 --- a/eck.module +++ b/eck.module @@ -91,6 +91,7 @@ function eck_entity_type_build(array &$entity_types) { 'label' => 'title', 'uuid' => 'uuid', 'langcode' => 'langcode', + 'published' => 'status', ], 'translatable' => TRUE, 'provider' => 'eck', diff --git a/src/Entity/EckEntity.php b/src/Entity/EckEntity.php index f24cadc..3a5cd1c 100644 --- a/src/Entity/EckEntity.php +++ b/src/Entity/EckEntity.php @@ -3,6 +3,8 @@ namespace Drupal\eck\Entity; use Drupal\Core\Entity\ContentEntityBase; +use Drupal\Core\Entity\EntityPublishedInterface; +use Drupal\Core\Entity\EntityPublishedTrait; use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Field\BaseFieldDefinition; @@ -14,7 +16,17 @@ use Drupal\user\UserInterface; * * @ingroup eck */ -class EckEntity extends ContentEntityBase implements EckEntityInterface { +class EckEntity extends ContentEntityBase implements EckEntityInterface, EntityPublishedInterface { + + use EntityPublishedTrait; + + /** + * {@inheritdoc} + */ + public function __construct(array $values, $entity_type, $bundle = FALSE, $translations = []) { + parent::__construct($values, $entity_type, $bundle, $translations); + $this->entityKeys['published'] = 'status'; + } /** * {@inheritdoc} @@ -176,6 +188,22 @@ class EckEntity extends ContentEntityBase implements EckEntityInterface { ->setDisplayConfigurable('view', TRUE); } + // Status field for the entity. + if ($config->get('status')) { + $fields += static::publishedBaseFieldDefinitions($entityType); + $fields['status'] + ->setLabel(t('Published')) + ->setInitialValue(TRUE) + ->setDisplayConfigurable('form', TRUE) + ->setDisplayOptions('form', [ + 'type' => 'boolean_checkbox', + 'settings' => [ + 'display_label' => TRUE, + ], + 'weight' => 100, + ]); + } + return $fields; } diff --git a/src/Entity/EckEntityType.php b/src/Entity/EckEntityType.php index 0241a78..af74cc1 100644 --- a/src/Entity/EckEntityType.php +++ b/src/Entity/EckEntityType.php @@ -36,7 +36,8 @@ use Drupal\eck\EckEntityTypeInterface; * "created", * "changed", * "uid", - * "title" + * "title", + * "status" * } * ) * diff --git a/src/Form/EntityBundle/EckEntityBundleForm.php b/src/Form/EntityBundle/EckEntityBundleForm.php index 0405c96..71cf8dd 100644 --- a/src/Form/EntityBundle/EckEntityBundleForm.php +++ b/src/Form/EntityBundle/EckEntityBundleForm.php @@ -112,7 +112,7 @@ class EckEntityBundleForm extends EntityForm { $base_fields = $this->entityFieldManager->getBaseFieldDefinitions($type->getEntityType()->getBundleOf()); $bundle_fields = $this->entityFieldManager->getFieldDefinitions($entity_type_id, $type->id()); - foreach (['title', 'uid', 'created', 'changed'] as $field) { + foreach (['title', 'uid', 'created', 'changed', 'status'] as $field) { if (!empty($entity_type_config->get($field))) { if (!isset($form['title_overrides'])) { $form['title_overrides'] = [ @@ -199,7 +199,7 @@ class EckEntityBundleForm extends EntityForm { $bundle_fields = $this->entityFieldManager->getFieldDefinitions($type->getEntityType()->getBundleOf(), $type->id()); $base_fields = $this->entityFieldManager->getBaseFieldDefinitions($type->getEntityType()->getBundleOf()); - foreach (['created', 'changed', 'uid', 'title'] as $field) { + foreach (['created', 'changed', 'uid', 'title', 'status'] as $field) { if (!$form_state->hasValue($field . '_title_override')) { continue; } diff --git a/src/Form/EntityType/EckEntityTypeEditForm.php b/src/Form/EntityType/EckEntityTypeEditForm.php index 3a86590..47c6200 100644 --- a/src/Form/EntityType/EckEntityTypeEditForm.php +++ b/src/Form/EntityType/EckEntityTypeEditForm.php @@ -34,7 +34,9 @@ class EckEntityTypeEditForm extends EckEntityTypeFormBase { /** @var \Drupal\Core\Entity\EntityFieldManagerInterface $efm */ $definitions = $this->entityFieldManager->getBaseFieldDefinitions($this->entity->id()); - foreach (['title', 'created', 'changed', 'uid'] as $field) { + foreach (['title', 'created', 'changed', 'uid', 'status'] as $field) { + // Lock entity base field configuration in case when that field already + // contain some data. if (isset($definitions[$field]) && $fieldStorage->countFieldData($definitions[$field], TRUE)) { $form['base_fields'][$field]['#disabled'] = TRUE; } diff --git a/src/Form/EntityType/EckEntityTypeFormBase.php b/src/Form/EntityType/EckEntityTypeFormBase.php index 21853a8..80b8c6a 100644 --- a/src/Form/EntityType/EckEntityTypeFormBase.php +++ b/src/Form/EntityType/EckEntityTypeFormBase.php @@ -91,7 +91,7 @@ class EckEntityTypeFormBase extends EntityForm { ]; $config = \Drupal::config('eck.eck_entity_type.' . $eck_entity_type->id()); - foreach (['created', 'changed', 'uid', 'title'] as $field) { + foreach (['created', 'changed', 'uid', 'title', 'status'] as $field) { $title = $field === 'uid' ? 'author' : $field; $form['base_fields'][$field] = [ diff --git a/tests/src/Functional/DynamicBaseFieldTest.php b/tests/src/Functional/DynamicBaseFieldTest.php index 199b42c..c42e61c 100644 --- a/tests/src/Functional/DynamicBaseFieldTest.php +++ b/tests/src/Functional/DynamicBaseFieldTest.php @@ -16,7 +16,7 @@ class DynamicBaseFieldTest extends FunctionalTestBase { */ public function testBaseFieldCRUD() { // Create the entity type. - $type = $this->createEntityType(['uid', 'created', 'changed']); + $type = $this->createEntityType(['uid', 'created', 'changed', 'status']); $bundle = $this->createEntityBundle($type['id']); // Make sure base fields are added. @@ -27,6 +27,7 @@ class DynamicBaseFieldTest extends FunctionalTestBase { $this->drupalGet(Url::fromRoute('eck.entity.add', $route_args)); $this->assertSession()->fieldExists('uid[0][target_id]'); $this->assertSession()->fieldExists('created[0][value][date]'); + $this->assertSession()->fieldExists('status[value]'); // Add a field to the entity type. $edit = ['title' => TRUE]; @@ -46,6 +47,15 @@ class DynamicBaseFieldTest extends FunctionalTestBase { $this->drupalGet(Url::fromRoute('eck.entity.add', $route_args)); $this->assertSession()->fieldNotExists('created[0][value][date]'); + // Remove 'status' field from entity type. + $edit = ['status' => FALSE]; + $this->drupalPostForm(Url::fromRoute('entity.eck_entity_type.edit_form', ['eck_entity_type' => $type['id']]), $edit, t('Update @type', ['@type' => $type['label']])); + $this->assertSession()->responseContains((string) t('Entity type %label has been updated.', ['%label' => $type['label']])); + + // Check if 'status' field really removed. + $this->drupalGet(Url::fromRoute('eck.entity.add', $route_args)); + $this->assertSession()->fieldNotExists('status[value]'); + // Add an entity to make sure there is data in the title field. $edit = ['title[0][value]' => $this->randomMachineName()]; $this->drupalPostForm(Url::fromRoute('eck.entity.add', $route_args), $edit, t('Save')); diff --git a/tests/src/Functional/FunctionalTestBase.php b/tests/src/Functional/FunctionalTestBase.php index 6a1bc71..f9bc878 100644 --- a/tests/src/Functional/FunctionalTestBase.php +++ b/tests/src/Functional/FunctionalTestBase.php @@ -77,7 +77,7 @@ abstract class FunctionalTestBase extends BrowserTestBase { * The machine names of the configurable base fields. */ protected function getConfigurableBaseFields() { - return ['created', 'changed', 'uid', 'title']; + return ['created', 'changed', 'uid', 'title', 'status']; } /**