Skip to content

Commit

Permalink
HPC-9966: Fix issues with extraction of current base object
Browse files Browse the repository at this point in the history
  • Loading branch information
berliner committed Dec 9, 2024
1 parent 907ada9 commit 3aed0f8
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 20 deletions.
22 changes: 12 additions & 10 deletions html/modules/custom/ghi_blocks/src/Plugin/Block/GHIBlockBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Drupal\Core\Render\Element;
use Drupal\Core\Render\Markup;
use Drupal\Core\Url;
use Drupal\ghi_base_objects\Entity\BaseObjectAwareEntityInterface;
use Drupal\ghi_base_objects\Entity\BaseObjectInterface;
use Drupal\ghi_base_objects\Helpers\BaseObjectHelper;
use Drupal\ghi_blocks\Form\ImportBlockForm;
Expand Down Expand Up @@ -1584,19 +1585,19 @@ public function getPageNode() {
*/
public function getPageEntity($expected_class = ContentEntityInterface::class) {
$page_arguments = $this->getAllAvailablePageParameters();
$page_entity = NULL;
if (!empty($page_arguments['section_storage']) && $page_arguments['section_storage'] instanceof SectionStorageInterface) {
/** @var \Drupal\layout_builder\SectionStorageInterface $section_storage */
$section_storage = $page_arguments['section_storage'];
$section_contexts = array_keys($section_storage->getContexts());
$entity = in_array('entity', $section_contexts) ? $section_storage->getContextValue('entity') : NULL;
return $entity instanceof $expected_class ? $entity : NULL;
$page_entity = $entity instanceof $expected_class ? $entity : NULL;
}
if (!empty($page_arguments['node'])) {
return $page_arguments['node'];
}
if (!empty($page_arguments['node_from_original_id'])) {
return $page_arguments['node_from_original_id'];

if ($page_entity) {
return $page_entity;
}

foreach ($page_arguments as $page_argument) {
if (!$page_argument instanceof $expected_class) {
continue;
Expand Down Expand Up @@ -1659,11 +1660,12 @@ public function getCurrentBaseEntity($page_node = NULL) {
*/
public function getCurrentBaseObject($entity = NULL) {
$page_entity = $entity instanceof ContentEntityInterface ? $entity : $this->getPageEntity();
if ($page_entity && $page_entity->hasField('field_base_object')) {
return $page_entity->get('field_base_object')->entity;
if ($page_entity instanceof BaseObjectAwareEntityInterface) {
return $page_entity->getBaseObject();
}
elseif (($base_page = $this->getCurrentBaseEntity($page_entity)) && $base_page->hasField('field_base_object')) {
return $base_page->get('field_base_object')->entity;
$base_page = $this->getCurrentBaseEntity($page_entity);
if ($base_page instanceof BaseObjectAwareEntityInterface) {
return $base_page->getBaseObject();
}
$contexts = $this->getContexts();
foreach ($this->getContextMapping() as $context_name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Drupal\Core\Cache\Cache;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Markup;
use Drupal\ghi_base_objects\Entity\BaseObjectAwareEntityInterface;
use Drupal\ghi_blocks\Interfaces\ConfigValidationInterface;
use Drupal\ghi_blocks\Interfaces\MultiStepFormBlockInterface;
use Drupal\ghi_blocks\Interfaces\OverrideDefaultTitleBlockInterface;
Expand Down Expand Up @@ -1206,7 +1205,7 @@ public function getBlockContext() {
return [
'page_node' => $page_node,
'plan_object' => $this->getCurrentPlanObject(),
'base_object' => $this->getCurrentBaseObject($page_node instanceof BaseObjectAwareEntityInterface ? $page_node : NULL),
'base_object' => $this->getCurrentBaseObject(),
'context_node' => $page_node,
'attachment_prototype' => $this->getAttachmentPrototype(),
];
Expand Down Expand Up @@ -1340,8 +1339,14 @@ public function fixConfigErrors() {
}
}

$default_attachment = array_key_first($conf['attachments']['entity_attachments']['attachments']['attachment_id']);
$conf['map']['common']['default_attachment'] = $default_attachment;
// Check the configured default attachment.
$default_attachment = $conf['map']['common']['default_attachment'] ?? NULL;
$attachment_ids = $conf['attachments']['entity_attachments']['attachments']['attachment_id'] ?? [];
if ($default_attachment && !array_key_exists($default_attachment, $attachment_ids)) {
// Just unset the default attachment, so that the rendering can decide
// which one to use.
$conf['map']['common']['default_attachment'] = NULL;
}

$this->setBlockConfig($conf);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Markup;
use Drupal\Core\Security\TrustedCallbackInterface;
use Drupal\ghi_base_objects\Entity\BaseObjectAwareEntityInterface;
use Drupal\ghi_blocks\Interfaces\ConfigValidationInterface;
use Drupal\ghi_blocks\Interfaces\ConfigurableTableBlockInterface;
use Drupal\ghi_blocks\Interfaces\CustomLinkBlockInterface;
Expand Down Expand Up @@ -613,8 +612,7 @@ private function getEntityRefCodeOptions() {
* An array of plan entity objects for the current context.
*/
private function getPlanEntities($entity_ref_code = NULL) {
$page_node = $this->getPageNode();
$context_object = $this->getCurrentBaseObject($page_node instanceof BaseObjectAwareEntityInterface ? $page_node : NULL);
$context_object = $this->getCurrentBaseObject();

if ($entity_ref_code == 'PL' && $context_object instanceof Plan) {
/** @var \Drupal\ghi_plans\Plugin\EndpointQuery\EntityQuery $query */
Expand Down Expand Up @@ -737,7 +735,7 @@ public function getBlockContext() {
'section_node' => $section_node,
'page_node' => $page_node,
'plan_object' => $plan_object,
'base_object' => $this->getCurrentBaseObject($page_node instanceof BaseObjectAwareEntityInterface ? $page_node : NULL),
'base_object' => $this->getCurrentBaseObject(),
'context_node' => $page_node,
'entities' => $plan_entities,
'entity_types' => $this->logframeManager->getEntityTypesFromPlanObject($plan_object),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\ghi_base_objects\Entity\BaseObjectAwareEntityInterface;
use Drupal\ghi_blocks\Interfaces\ConfigValidationInterface;
use Drupal\ghi_blocks\Interfaces\ConfigurableTableBlockInterface;
use Drupal\ghi_blocks\Interfaces\MultiStepFormBlockInterface;
Expand Down Expand Up @@ -220,7 +219,7 @@ public function getBlockContext() {
'section_node' => $this->getCurrentBaseEntity(),
'page_node' => $page_node,
'plan_object' => $this->getCurrentPlanObject(),
'base_object' => $this->getCurrentBaseObject($page_node instanceof BaseObjectAwareEntityInterface ? $page_node : NULL),
'base_object' => $this->getCurrentBaseObject(),
'context_node' => $page_node,
];
}
Expand Down

0 comments on commit 3aed0f8

Please sign in to comment.