From ac4e6eddd50da8b1a116d6cf220b8513767b9cb3 Mon Sep 17 00:00:00 2001 From: naomi Date: Mon, 13 Sep 2021 14:15:02 +0100 Subject: [PATCH] Made published field configurable on forms, and made views respect permissions --- modules/opencase_cases/src/Entity/OCCase.php | 1 + .../opencase_entities/src/Entity/OCActivity.php | 2 +- modules/opencase_entities/src/Entity/OCActor.php | 2 +- modules/opencase_entities/src/Entity/OCEvent.php | 9 ++++++++- .../src/Entity/OCOrganisation.php | 7 ++++++- opencase.module | 14 ++++++++++++++ 6 files changed, 31 insertions(+), 4 deletions(-) diff --git a/modules/opencase_cases/src/Entity/OCCase.php b/modules/opencase_cases/src/Entity/OCCase.php index 9e72375..e6d3991 100644 --- a/modules/opencase_cases/src/Entity/OCCase.php +++ b/modules/opencase_cases/src/Entity/OCCase.php @@ -208,6 +208,7 @@ class OCCase extends RevisionableContentEntityBase implements OCCaseInterface { ->setLabel(t('Publishing status')) ->setDescription(t('A boolean indicating whether the Case is published.')) ->setRevisionable(TRUE) + ->setDisplayConfigurable("form", TRUE) ->setDefaultValue(TRUE); $fields['user_id'] = BaseFieldDefinition::create('entity_reference') diff --git a/modules/opencase_entities/src/Entity/OCActivity.php b/modules/opencase_entities/src/Entity/OCActivity.php index a00a545..b4adfb5 100644 --- a/modules/opencase_entities/src/Entity/OCActivity.php +++ b/modules/opencase_entities/src/Entity/OCActivity.php @@ -240,11 +240,11 @@ class OCActivity extends RevisionableContentEntityBase implements OCActivityInte 'weight' => -3, ]); - // not currently in use. Will set view and form settings when ready $fields['status'] = BaseFieldDefinition::create('boolean') ->setLabel(t('Publishing status')) ->setDescription(t('A boolean indicating whether the Activity is published.')) ->setRevisionable(TRUE) + ->setDisplayConfigurable('form', TRUE) ->setDefaultValue(TRUE); $fields['user_id'] = BaseFieldDefinition::create('entity_reference') diff --git a/modules/opencase_entities/src/Entity/OCActor.php b/modules/opencase_entities/src/Entity/OCActor.php index 4e56686..b9cbd75 100644 --- a/modules/opencase_entities/src/Entity/OCActor.php +++ b/modules/opencase_entities/src/Entity/OCActor.php @@ -213,11 +213,11 @@ class OCActor extends RevisionableContentEntityBase implements OCActorInterface public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { $fields = parent::baseFieldDefinitions($entity_type); - // Currently not using this, but will add form and view settings when ready. $fields['status'] = BaseFieldDefinition::create('boolean') ->setLabel(t('Publishing status')) ->setDescription(t('Whether this record is published.')) ->setRevisionable(TRUE) + ->setDisplayConfigurable("form", true) ->setDefaultValue(TRUE); // The name gets set on preSave, from the first middle and last diff --git a/modules/opencase_entities/src/Entity/OCEvent.php b/modules/opencase_entities/src/Entity/OCEvent.php index 10e8f69..26dea7f 100644 --- a/modules/opencase_entities/src/Entity/OCEvent.php +++ b/modules/opencase_entities/src/Entity/OCEvent.php @@ -167,7 +167,14 @@ class OCEvent extends ContentEntityBase implements OCEventInterface { ->setDisplayConfigurable('view', TRUE) ->setRequired(TRUE); - $fields['status']->setDescription(t('A boolean indicating whether the Event is published.')); + $fields['status'] = BaseFieldDefinition::create('boolean') + ->setLabel(t('Publishing status')) + ->setDescription(t('Whether this record is published.')) + ->setRevisionable(TRUE) + ->setDisplayConfigurable("form", true) + ->setDefaultValue(TRUE); + + $fields['created'] = BaseFieldDefinition::create('created') ->setLabel(t('Created')) diff --git a/modules/opencase_entities/src/Entity/OCOrganisation.php b/modules/opencase_entities/src/Entity/OCOrganisation.php index b805251..12f21f9 100644 --- a/modules/opencase_entities/src/Entity/OCOrganisation.php +++ b/modules/opencase_entities/src/Entity/OCOrganisation.php @@ -456,7 +456,12 @@ class OCOrganisation extends EditorialContentEntityBase implements OCOrganisatio ->setDisplayConfigurable('view', TRUE) ->setRequired(TRUE); - $fields['status']->setDescription(t('A boolean indicating whether the Organisation is published.')); + $fields['status'] = BaseFieldDefinition::create('boolean') + ->setLabel(t('Published')) + ->setRevisionable(TRUE) + ->setDisplayConfigurable("form", true) + ->setDefaultValue(TRUE); + $fields['created'] = BaseFieldDefinition::create('created') ->setLabel(t('Created')) diff --git a/opencase.module b/opencase.module index 64f290d..b22bc1e 100644 --- a/opencase.module +++ b/opencase.module @@ -98,6 +98,20 @@ function opencase_uninstall() { } function opencase_views_pre_render($view) { + if (!empty($view->result)) { + foreach ($view->result as $key => $result) { + if (empty($result->_entity)) { + continue; + } + $access = \Drupal::entityTypeManager() + ->getAccessControlHandler($result->_entity->getEntityTypeId()) + ->access($result->_entity, 'view', NULL, TRUE); + + if (!$access->isAllowed()) { + unset($view->result[$key]); + } + } + } if (empty($view->result) && empty($view->exposed_input)) { $view->exposed_widgets = NULL; }