diff --git a/modules/custom/az_core/az_core.services.yml b/modules/custom/az_core/az_core.services.yml index 7a7f676e9c..0331325d53 100755 --- a/modules/custom/az_core/az_core.services.yml +++ b/modules/custom/az_core/az_core.services.yml @@ -33,4 +33,5 @@ services: # externalauth.authmap declared optional because CAS may or may not be enabled. arguments: - '@entity_type.manager' + - '@entity_field.manager' - '@?externalauth.authmap' diff --git a/modules/custom/az_core/src/AZUserToolbarLinkBuilder.php b/modules/custom/az_core/src/AZUserToolbarLinkBuilder.php index d1e5dc9d83..17e49d7a9d 100644 --- a/modules/custom/az_core/src/AZUserToolbarLinkBuilder.php +++ b/modules/custom/az_core/src/AZUserToolbarLinkBuilder.php @@ -2,6 +2,7 @@ namespace Drupal\az_core; +use Drupal\Core\Entity\EntityFieldManagerInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Session\AccountProxyInterface; use Drupal\Core\Url; @@ -27,6 +28,13 @@ class AZUserToolbarLinkBuilder extends ToolbarLinkBuilder { */ protected $entityTypeManager; + /** + * Entity field manager service definition. + * + * @var \Drupal\Core\Entity\EntityFieldManagerInterface + */ + protected $entityFieldManager; + /** * ToolbarHandler constructor. * @@ -34,12 +42,20 @@ class AZUserToolbarLinkBuilder extends ToolbarLinkBuilder { * The current user. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager * The entity type manager. + * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager + * The entity field manager. * @param \Drupal\externalauth\AuthmapInterface $authmap * The authmap service. */ - public function __construct(AccountProxyInterface $account, EntityTypeManagerInterface $entityTypeManager, ?AuthmapInterface $authmap) { + public function __construct( + AccountProxyInterface $account, + EntityTypeManagerInterface $entityTypeManager, + EntityFieldManagerInterface $entityFieldManager, + ?AuthmapInterface $authmap, + ) { parent::__construct($account); $this->entityTypeManager = $entityTypeManager; + $this->entityFieldManager = $entityFieldManager; $this->authmap = $authmap; } @@ -58,31 +74,35 @@ public function renderToolbarLinks() { if ($this->account->hasPermission('edit matching netid content')) { $auth = $this->authmap->get($this->account->id(), 'cas'); if (($auth !== FALSE)) { - // Check if we have a linked person. - $persons = $this->entityTypeManager->getStorage('node')->loadByProperties([ - 'field_az_netid' => $auth, - 'type' => 'az_person', - 'status' => [1, TRUE], - ]); - if (!empty($persons)) { - $person = reset($persons); - // If we have a linked az person, generate links. - $additional_links = [ - 'az_person' => [ - 'title' => $this->t('View my web page'), - 'url' => Url::fromRoute('entity.node.canonical', ['node' => $person->id()]), - 'attributes' => [ + // Verify that 'field_az_netid' exists for 'az_person' content type. + $field_definitions = $this->entityFieldManager->getFieldDefinitions('node', 'az_person'); + if (isset($field_definitions['field_az_netid'])) { + // Check if we have a linked person. + $persons = $this->entityTypeManager->getStorage('node')->loadByProperties([ + 'field_az_netid' => $auth, + 'type' => 'az_person', + 'status' => [1, TRUE], + ]); + if (!empty($persons)) { + $person = reset($persons); + // If we have a linked az person, generate links. + $additional_links = [ + 'az_person' => [ 'title' => $this->t('View my web page'), + 'url' => Url::fromRoute('entity.node.canonical', ['node' => $person->id()]), + 'attributes' => [ + 'title' => $this->t('View my web page'), + ], ], - ], - 'az_person_edit' => [ - 'title' => $this->t('Edit my web page'), - 'url' => Url::fromRoute('entity.node.edit_form', ['node' => $person->id()]), - 'attributes' => [ + 'az_person_edit' => [ 'title' => $this->t('Edit my web page'), + 'url' => Url::fromRoute('entity.node.edit_form', ['node' => $person->id()]), + 'attributes' => [ + 'title' => $this->t('Edit my web page'), + ], ], - ], - ]; + ]; + } } } } @@ -100,6 +120,7 @@ public function renderToolbarLinks() { $links = array_merge($additional_links, $original_links); $build['#links'] = $links; } + return $build; }