Add PlanRecordInterface::getPlan()

This commit is contained in:
Paul Weidner 2024-01-31 14:24:01 -08:00 committed by Michael Stenta
parent bd091b7d39
commit 06b930886a
3 changed files with 18 additions and 4 deletions

View File

@ -18,10 +18,9 @@ class PlanRecordAccess extends EntityAccessControlHandler {
protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
// If a plan is referenced, access is based on access to the plan.
/** @var \Drupal\plan\Entity\PlanInterface[] $plans */
$plans = $entity->get('plan')->referencedEntities();
if (!empty($plans[0])) {
return AccessResult::allowedIf($plans[0]->access($operation, $account));
/** @var \Drupal\plan\Entity\PlanRecordInterface $plan */
if ($plan = $entity->getPlan()) {
return AccessResult::allowedIf($plan->access($operation, $account));
}
// Otherwise, delegate to the parent method.

View File

@ -82,4 +82,11 @@ class PlanRecord extends ContentEntityBase implements PlanRecordInterface {
return $fields;
}
/**
* {@inheritdoc}
*/
public function getPlan(): ?PlanInterface {
return $this->get('plan')->first()?->entity;
}
}

View File

@ -17,4 +17,12 @@ interface PlanRecordInterface extends ContentEntityInterface {
*/
public function getBundleLabel();
/**
* Returns the Plan entity the plan record is assigned to.
*
* @return \Drupal\plan\Entity\PlanInterface|null
* The plant entity or NULL if not assigned.
*/
public function getPlan(): ?PlanInterface;
}