CatalogLinkDerivative.php in Commerce Demo 8.2
File
src/Plugin/Derivative/CatalogLinkDerivative.php
View source
<?php
namespace Drupal\commerce_demo\Plugin\Derivative;
use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Drupal\taxonomy\TermInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
final class CatalogLinkDerivative extends DeriverBase implements ContainerDeriverInterface {
protected $termStorage;
public function __construct(EntityTypeManagerInterface $entityTypeManager) {
$this->termStorage = $entityTypeManager
->getStorage('taxonomy_term');
}
public static function create(ContainerInterface $container, $base_plugin_id) {
return new static($container
->get('entity_type.manager'));
}
public function getDerivativeDefinitions($base_plugin_definition) {
$this->derivatives = [];
$terms = $this->termStorage
->loadTree('product_categories', 0, NULL, TRUE);
foreach ($terms as $term) {
assert($term instanceof TermInterface);
if (!$term
->isPublished()) {
continue;
}
$parents = $this->termStorage
->loadParents($term
->id());
$parent = reset($parents);
if ($parent && !$parent
->isPublished()) {
continue;
}
$menu_link_parent = NULL;
if ($parent) {
$menu_link_parent = 'catalog:' . $parent
->uuid();
}
$this->derivatives[$term
->uuid()] = [
'route_name' => 'entity.taxonomy_term.canonical',
'route_parameters' => [
'taxonomy_term' => $term
->id(),
],
'title' => $term
->label(),
'expanded' => 1,
'menu_name' => 'main',
'parent' => $menu_link_parent,
] + $base_plugin_definition;
}
return $this->derivatives;
}
}