Commit 8651f790 authored by Jamie Hollern's avatar Jamie Hollern Committed by Tim Bozeman
Browse files

Issue #3301357 by jamiehollern, Tim Bozeman: Remove existing location options...

Issue #3301357 by jamiehollern, Tim Bozeman: Remove existing location options on the add localization form location select element
parent 008f0d0a
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -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']
+2 −2
Original line number Diff line number Diff line
@@ -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,
    ];
@@ -85,7 +85,7 @@ class AddLocation extends FormBase {
    ], [
      'query' => [
        'locations' => $locations,
      ]
      ],
    ]);
  }

+45 −1
Original line number Diff line number Diff line
@@ -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;

/**
@@ -20,6 +21,7 @@ class LocationHandler {
   */
  protected array $locationHierarchy = [];


  /**
   * @var \Drupal\location_variant\Location[]
   */
@@ -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;
  }

  /**
@@ -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.
   *