Loading modules/location_variant/location_variant.services.yml +1 −1 Original line number Diff line number Diff line Loading @@ -6,7 +6,7 @@ services: - { name: variant_negotiator, priority: 30 } location_variant.location_handler: class: Drupal\location_variant\LocationHandler arguments: ['@event_dispatcher', '@entity_type.manager'] arguments: ['@event_dispatcher', '@entity_type.manager', '@request_stack', '@path.current', '@workspaces.manager'] location_variant.entity_handler: class: Drupal\location_variant\EntityLocationHandler arguments: ['@variant_handler', '@entity_type.manager', '@location_variant.location_handler', '@current_route_match', '@request_stack'] Loading modules/location_variant/src/Form/AddLocation.php +2 −2 Original line number Diff line number Diff line Loading @@ -48,7 +48,7 @@ class AddLocation extends FormBase { $form['locations'] = [ '#type' => 'select', '#title' => $this->t('Location'), '#options' => $this->locationHandler->getLocationOptions(), '#options' => $this->locationHandler->getAvailableLocationOptions(), '#required' => TRUE, '#multiple' => TRUE, ]; Loading Loading @@ -85,7 +85,7 @@ class AddLocation extends FormBase { ], [ 'query' => [ 'locations' => $locations, ] ], ]); } Loading modules/location_variant/src/LocationHandler.php +45 −1 Original line number Diff line number Diff line Loading @@ -4,6 +4,7 @@ namespace Drupal\location_variant; use Drupal\location_variant\Event\LocationsEvent; use Drupal\Core\Entity\EntityTypeManagerInterface; use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\EventDispatcher\EventDispatcherInterface; /** Loading @@ -20,6 +21,7 @@ class LocationHandler { */ protected array $locationHierarchy = []; /** * @var \Drupal\location_variant\Location[] */ Loading @@ -27,10 +29,12 @@ class LocationHandler { protected EventDispatcherInterface $dispatcher; protected EntityTypeManagerInterface $entityTypeManager; protected RequestStack $requestStack; public function __construct(EventDispatcherInterface $dispatcher, EntityTypeManagerInterface $entityTypeManager) { public function __construct(EventDispatcherInterface $dispatcher, EntityTypeManagerInterface $entityTypeManager, RequestStack $requestStack) { $this->dispatcher = $dispatcher; $this->entityTypeManager = $entityTypeManager; $this->requestStack = $requestStack; } /** Loading Loading @@ -120,6 +124,46 @@ class LocationHandler { return $options; } /** * Get an array of location options, excluding locations that already exist. * * @param bool $rebuild * Flag to rebuild locations. * * @return array * An array of location options. * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException */ public function getAvailableLocationOptions(bool $rebuild = FALSE): array { // Get the current entity ID. $request = $this->requestStack->getCurrentRequest(); $entity_type = $request->attributes->get('entity_type_id'); $entity = $request->attributes->get($entity_type); $entity_id = $entity->id(); // Find any variants belonging to the current entity ID. $location_variants = $this->entityTypeManager->getStorage('variant')->loadByProperties(['entity_id' => $entity_id,]); // Return the default location options if we found no variants. $location_options = $this->getLocationOptions($rebuild); if (!$location_variants) { return $location_options; } // If we have variants, get their locations and remove them from the // locations array. $extant_locations = []; foreach ($location_variants as $location) { $location_code = $location->get('locations')->getValue()[0]['value'] ?? ''; if (!$location_code) { continue; } $extant_locations[$location_code] = $location_code; } return array_filter($location_options, fn($location_code) => !in_array($location_code, $extant_locations), ARRAY_FILTER_USE_KEY); } /** * Append child options. * Loading Loading
modules/location_variant/location_variant.services.yml +1 −1 Original line number Diff line number Diff line Loading @@ -6,7 +6,7 @@ services: - { name: variant_negotiator, priority: 30 } location_variant.location_handler: class: Drupal\location_variant\LocationHandler arguments: ['@event_dispatcher', '@entity_type.manager'] arguments: ['@event_dispatcher', '@entity_type.manager', '@request_stack', '@path.current', '@workspaces.manager'] location_variant.entity_handler: class: Drupal\location_variant\EntityLocationHandler arguments: ['@variant_handler', '@entity_type.manager', '@location_variant.location_handler', '@current_route_match', '@request_stack'] Loading
modules/location_variant/src/Form/AddLocation.php +2 −2 Original line number Diff line number Diff line Loading @@ -48,7 +48,7 @@ class AddLocation extends FormBase { $form['locations'] = [ '#type' => 'select', '#title' => $this->t('Location'), '#options' => $this->locationHandler->getLocationOptions(), '#options' => $this->locationHandler->getAvailableLocationOptions(), '#required' => TRUE, '#multiple' => TRUE, ]; Loading Loading @@ -85,7 +85,7 @@ class AddLocation extends FormBase { ], [ 'query' => [ 'locations' => $locations, ] ], ]); } Loading
modules/location_variant/src/LocationHandler.php +45 −1 Original line number Diff line number Diff line Loading @@ -4,6 +4,7 @@ namespace Drupal\location_variant; use Drupal\location_variant\Event\LocationsEvent; use Drupal\Core\Entity\EntityTypeManagerInterface; use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\EventDispatcher\EventDispatcherInterface; /** Loading @@ -20,6 +21,7 @@ class LocationHandler { */ protected array $locationHierarchy = []; /** * @var \Drupal\location_variant\Location[] */ Loading @@ -27,10 +29,12 @@ class LocationHandler { protected EventDispatcherInterface $dispatcher; protected EntityTypeManagerInterface $entityTypeManager; protected RequestStack $requestStack; public function __construct(EventDispatcherInterface $dispatcher, EntityTypeManagerInterface $entityTypeManager) { public function __construct(EventDispatcherInterface $dispatcher, EntityTypeManagerInterface $entityTypeManager, RequestStack $requestStack) { $this->dispatcher = $dispatcher; $this->entityTypeManager = $entityTypeManager; $this->requestStack = $requestStack; } /** Loading Loading @@ -120,6 +124,46 @@ class LocationHandler { return $options; } /** * Get an array of location options, excluding locations that already exist. * * @param bool $rebuild * Flag to rebuild locations. * * @return array * An array of location options. * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException */ public function getAvailableLocationOptions(bool $rebuild = FALSE): array { // Get the current entity ID. $request = $this->requestStack->getCurrentRequest(); $entity_type = $request->attributes->get('entity_type_id'); $entity = $request->attributes->get($entity_type); $entity_id = $entity->id(); // Find any variants belonging to the current entity ID. $location_variants = $this->entityTypeManager->getStorage('variant')->loadByProperties(['entity_id' => $entity_id,]); // Return the default location options if we found no variants. $location_options = $this->getLocationOptions($rebuild); if (!$location_variants) { return $location_options; } // If we have variants, get their locations and remove them from the // locations array. $extant_locations = []; foreach ($location_variants as $location) { $location_code = $location->get('locations')->getValue()[0]['value'] ?? ''; if (!$location_code) { continue; } $extant_locations[$location_code] = $location_code; } return array_filter($location_options, fn($location_code) => !in_array($location_code, $extant_locations), ARRAY_FILTER_USE_KEY); } /** * Append child options. * Loading