Skip to content

Commit

Permalink
Fixes #3946 Whitescreen if az_person not installed. (#3947)
Browse files Browse the repository at this point in the history
Co-authored-by: Joe Parsons <[email protected]>
  • Loading branch information
trackleft and joeparsons authored Dec 13, 2024
1 parent e67998e commit 4e75641
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 22 deletions.
1 change: 1 addition & 0 deletions modules/custom/az_core/az_core.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
65 changes: 43 additions & 22 deletions modules/custom/az_core/src/AZUserToolbarLinkBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,19 +28,34 @@ class AZUserToolbarLinkBuilder extends ToolbarLinkBuilder {
*/
protected $entityTypeManager;

/**
* Entity field manager service definition.
*
* @var \Drupal\Core\Entity\EntityFieldManagerInterface
*/
protected $entityFieldManager;

/**
* ToolbarHandler constructor.
*
* @param \Drupal\Core\Session\AccountProxyInterface $account
* 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;
}

Expand All @@ -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'),
],
],
],
];
];
}
}
}
}
Expand All @@ -100,6 +120,7 @@ public function renderToolbarLinks() {
$links = array_merge($additional_links, $original_links);
$build['#links'] = $links;
}

return $build;
}

Expand Down

0 comments on commit 4e75641

Please sign in to comment.