Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #3946 Whitescreen if az_person not installed. #3947

Merged
merged 16 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading