diff --git a/config/schema/while_entity_type.schema.yml b/config/schema/while_entity_type.schema.yml index 58f4dd0..b3571a2 100644 --- a/config/schema/while_entity_type.schema.yml +++ b/config/schema/while_entity_type.schema.yml @@ -10,3 +10,6 @@ white_label_entity.while_entity_type.*: label: 'Label' uuid: type: string + entity_pages_active: + type: boolean + label: 'Provide entity pages' diff --git a/src/Controller/WhileEntityController.php b/src/Controller/WhileEntityController.php index 1764f47..21edbcf 100644 --- a/src/Controller/WhileEntityController.php +++ b/src/Controller/WhileEntityController.php @@ -7,9 +7,11 @@ use Drupal\Core\Controller\ControllerBase; use Drupal\Core\DependencyInjection\ContainerInjectionInterface; use Drupal\Core\Url; use Drupal\white_label_entity\Entity\WhileEntityInterface; +use Drupal\white_label_entity\Entity\WhileEntityType; use Drupal\Core\Datetime\DateFormatter; use Drupal\Core\Render\Renderer; use Symfony\Component\DependencyInjection\ContainerInterface; +use Drupal\Core\Access\AccessResult; /** * Class WhileEntityController. @@ -201,4 +203,21 @@ class WhileEntityController extends ControllerBase implements ContainerInjection return $build; } + /** + * Handles entity page access by entity type configuration. + * + * @param WhileEntity $while_entity + * @return AccessResult + */ + public function entityPageAccess(WhileEntityInterface $while_entity) { + $entity_type = $while_entity->type->entity; + + if ($entity_type->get('entity_pages_active')) { + return $while_entity->access('view', NULL, TRUE); + } + else { + return AccessResult::forbidden(); + } + } + } diff --git a/src/Entity/WhileEntityType.php b/src/Entity/WhileEntityType.php index b50d9f2..a13e7a7 100644 --- a/src/Entity/WhileEntityType.php +++ b/src/Entity/WhileEntityType.php @@ -27,7 +27,7 @@ use Drupal\Core\Config\Entity\ConfigEntityBundleBase; * entity_keys = { * "id" = "id", * "label" = "label", - * "uuid" = "uuid" + * "uuid" = "uuid", * } * ) */ @@ -47,4 +47,18 @@ class WhileEntityType extends ConfigEntityBundleBase implements WhileEntityTypeI */ protected $label; + /** + * The state of entity page display. + * + * @var bool + */ + protected $entity_pages_active = 0; + + /** + * {@inheritdoc} + */ + public function getEntityPagesActive() { + return $this->get('entity_pages_active'); + } + } diff --git a/src/Entity/WhileEntityTypeInterface.php b/src/Entity/WhileEntityTypeInterface.php index c2010eb..2245e98 100644 --- a/src/Entity/WhileEntityTypeInterface.php +++ b/src/Entity/WhileEntityTypeInterface.php @@ -9,5 +9,11 @@ use Drupal\Core\Config\Entity\ConfigEntityInterface; */ interface WhileEntityTypeInterface extends ConfigEntityInterface { - // Add get/set methods for your configuration properties here. + /** + * Gets the entity pages active state. + * + * @return bool + */ + public function getEntityPagesActive(); + } diff --git a/src/Form/WhileEntityForm.php b/src/Form/WhileEntityForm.php index ca48556..01d621d 100644 --- a/src/Form/WhileEntityForm.php +++ b/src/Form/WhileEntityForm.php @@ -66,7 +66,14 @@ class WhileEntityForm extends ContentEntityForm { '@entity_name' => $entity->getEntityType()->getSingularLabel(), ])); } - $form_state->setRedirect('entity.while_entity.canonical', ['while_entity' => $entity->id()]); + + $entity_type = $entity->type->entity; + if ($entity_type->get('entity_pages_active')) { + $form_state->setRedirect('entity.while_entity.canonical', ['while_entity' => $entity->id()]); + } + else { + $form_state->setRedirect('entity.while_entity.collection'); + } } } diff --git a/src/Form/WhileEntityTypeForm.php b/src/Form/WhileEntityTypeForm.php index 75466f1..c07cece 100644 --- a/src/Form/WhileEntityTypeForm.php +++ b/src/Form/WhileEntityTypeForm.php @@ -37,7 +37,11 @@ class WhileEntityTypeForm extends EntityForm { '#disabled' => !$while_entity_type->isNew(), ]; - /* You will need additional form elements for your custom properties. */ + $form['entity_pages_active'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Provide entity pages'), + '#default_value' => $while_entity_type->getEntityPagesActive(), + ]; return $form; } @@ -47,6 +51,9 @@ class WhileEntityTypeForm extends EntityForm { */ public function save(array $form, FormStateInterface $form_state) { $while_entity_type = $this->entity; + + $while_entity_type->set('entity_pages_active', $form_state->getValue('entity_pages_active')); + $status = $while_entity_type->save(); switch ($status) { diff --git a/src/WhileEntityHtmlRouteProvider.php b/src/WhileEntityHtmlRouteProvider.php index 6813cea..3c85c77 100644 --- a/src/WhileEntityHtmlRouteProvider.php +++ b/src/WhileEntityHtmlRouteProvider.php @@ -6,6 +6,7 @@ use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Entity\Routing\AdminHtmlRouteProvider; use Drupal\Core\StringTranslation\TranslatableMarkup; use Symfony\Component\Routing\Route; +use Drupal\white_label_entity\Entity\WhileEntityType; /** * Provides routes for while entities. @@ -23,6 +24,12 @@ class WhileEntityHtmlRouteProvider extends AdminHtmlRouteProvider { $entity_type_id = $entity_type->id(); + // Add custom access requirement to default canonical route. + $canonical_route = $collection->get("entity.{$entity_type_id}.canonical"); + $canonical_route->addRequirements([ + '_custom_access' => '\Drupal\white_label_entity\Controller\WhileEntityController::entityPageAccess' + ]); + if ($collection_route = $this->getCollectionRoute($entity_type)) { $collection->add("entity.{$entity_type_id}.collection", $collection_route); } diff --git a/src/WhileEntityListBuilder.php b/src/WhileEntityListBuilder.php index 0cc1297..5065cf9 100644 --- a/src/WhileEntityListBuilder.php +++ b/src/WhileEntityListBuilder.php @@ -102,14 +102,20 @@ class WhileEntityListBuilder extends EntityListBuilder { /* @var $entity \Drupal\white_label_entity\Entity\WhileEntity */ $row['created'] = $this->dateFormatter->format($entity->getCreatedTime(), 'short'); - $row['name'] = $this->l( - $entity->label(), - new Url( - 'entity.while_entity.edit_form', [ - 'while_entity' => $entity->id(), - ] - ) - ); + $access_manager = \Drupal::service('access_manager'); + if ($access_manager->checkNamedRoute('entity.while_entity.canonical', ['while_entity' => $entity->id()], \Drupal::currentUser())) { + $row['name'] = $this->l( + $entity->label(), + new Url( + 'entity.while_entity.canonical', [ + 'while_entity' => $entity->id(), + ] + ) + ); + } + else { + $row['name'] = $entity->label(); + } $row['author']['data'] = [ '#theme' => 'username',