Skip to content

Commit

Permalink
HPC-9281: Adds an 'Add team' link to the team list page and fixes the…
Browse files Browse the repository at this point in the history
… breadcrumbs
  • Loading branch information
berliner committed Mar 20, 2024
1 parent c81abd9 commit c4209af
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ public function build() {
],
];

// If we have a section context, we also want to add breadcrumps.
// If we have a section context, we also want to add breadcrumbs.
if ($section && $document) {
// For single chapter documents, we don't show the chapter title in the
// breadcrump.
// breadcrumb.
$single_chapter_document = count($document->getChapters(FALSE)) == 1;
if ($chapter = $node->getDocumentChapter($document)) {
$title_args = [
Expand Down
7 changes: 7 additions & 0 deletions html/modules/custom/ghi_teams/ghi_teams.links.action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ghi_teams.add_team:
route_name: entity.taxonomy_term.add_form
route_parameters:
taxonomy_vocabulary: team
title: Add team
appears_on:
- view.teams.page_teams
29 changes: 28 additions & 1 deletion html/modules/custom/ghi_teams/ghi_teams.module
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use Drupal\user\Entity\User;
use Drupal\user\UserInterface;
use Drupal\views\Plugin\Block\ViewsBlock;
use Drupal\views\Views;
use Symfony\Component\Routing\Exception\RouteNotFoundException;

/**
* Implements hook_entity_bundle_info_alter().
Expand Down Expand Up @@ -110,6 +111,32 @@ function ghi_teams_node_grants(AccountInterface $account, $op) {
return $grants;
}

/**
* Implements hook_menu_local_actions_alter().
*
* Add the destination argument to the add team action so that the user is
* redirected to the teams list page after creating a new team.
*/
function ghi_teams_menu_local_actions_alter(&$local_actions) {
if (empty($local_actions['ghi_teams.add_team'])) {
return;
}
$route_name = 'view.teams.page_teams';
try {
\Drupal::service('router.route_provider')
->getRouteByName($route_name);
}
catch (RouteNotFoundException $exception) {
return NULL;
}
$team_page_url = Url::fromRoute($route_name);
$local_actions['ghi_teams.add_team']['options'] = [
'query' => [
'destination' => $team_page_url->toString(),
],
];
}

/**
* Implements hook_FORM_ID_alter().
*
Expand All @@ -121,7 +148,7 @@ function ghi_teams_form_user_form_alter(&$form, FormStateInterface $form_state)
}

/**
* Implements hook_FORM_ID_alter().
* Implements hook_alter().
*
* Restrict access to field_team to users with proper permission.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\Entity\Vocabulary;

/**
* Breadcrumb builder for team backend pages.
Expand All @@ -21,7 +22,14 @@ class TeamsBreadcrumbBuilder implements BreadcrumbBuilderInterface {
*/
public function applies(RouteMatchInterface $route_match) {
$term = $route_match->getParameter('taxonomy_term');
return ($term instanceof Term && $term->bundle() == 'team');
if ($term instanceof Term && $term->bundle() == 'team') {
return TRUE;
}
$vocabulary = $route_match->getParameter('taxonomy_vocabulary');
if ($vocabulary instanceof Vocabulary && $vocabulary->id() == 'team') {
return TRUE;
}
return FALSE;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace Drupal\ghi_teams\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Routing\RouteMatchInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Controller class for taxonomy terms.
*
* The main thing this does, is to change the page titles for the different
* taxonomy pages (add, edit, delete, ...).
*
* @see \Drupal\ghi_teams\EventSubscriber\TaxonomyTermRouteSubscriber
*/
class TaxonomyTermController extends ControllerBase {

/**
* The route match.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected $routeMatch;

/**
* Public constructor.
*
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The route match.
*/
public function __construct(RouteMatchInterface $route_match) {
$this->routeMatch = $route_match;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
$instance = new static($container->get('current_route_match'));
return $instance;
}

/**
* Title callback for the term add page.
*
* @return \Drupal\Core\StringTranslation\TranslatableMarkup
* The title.
*/
public function addTitle() {
/** @var \Drupal\taxonomy\VocabularyInterface $term */
$vocabulary = $this->routeMatch->getParameter('taxonomy_vocabulary');
return $vocabulary ? $this->t('Add @label', [
'@label' => strtolower($vocabulary->label()),
]) : $this->t('Add term');
}

/**
* Title callback for the term edit page.
*
* @return \Drupal\Core\StringTranslation\TranslatableMarkup
* The title.
*/
public function editTitle() {
/** @var \Drupal\taxonomy\TermInterface $term */
$term = $this->routeMatch->getParameter('taxonomy_term');
return $term ? $this->t('Edit @label', [
'@label' => strtolower($term->vid->entity->label()),
]) : $this->t('Edit term');
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,36 @@
namespace Drupal\ghi_teams\EventSubscriber;

use Drupal\Core\Routing\RouteSubscriberBase;
use Drupal\ghi_teams\Controller\TaxonomyTermController;
use Symfony\Component\Routing\RouteCollection;

/**
* Sets the _admin_route for all taxonomy term routes.
* Route subscriber for taxonomy pages.
*
* Make sure that the taxonomy term pages are displayed using the admin theme.
* Set title callbacks for term form pages.
*
* @see \Drupal\ghi_teams\Controller\TaxonomyTermController
*/
class TaxonomyTermRouteSubscriber extends RouteSubscriberBase {

/**
* {@inheritdoc}
*/
protected function alterRoutes(RouteCollection $collection) {
// Sets the _admin_route for the canonical taxonomy term route.
if ($route = $collection->get('entity.taxonomy_term.canonical')) {
$route->setOption('_admin_route', TRUE);
}
if ($route = $collection->get('entity.taxonomy_term.add_form')) {
$route->setDefault('_title_callback', TaxonomyTermController::class . '::addTitle');
}
if ($route = $collection->get('entity.taxonomy_term.edit_form')) {
$route->setDefault('_title_callback', TaxonomyTermController::class . '::editTitle');
}
if ($route = $collection->get('entity.taxonomy_term.delete_form')) {
$route->setDefault('_title_callback', TaxonomyTermController::class . '::editTitle');
}
}

}

0 comments on commit c4209af

Please sign in to comment.