Verified Commit 91f59e70 authored by Lee Rowlands's avatar Lee Rowlands
Browse files

Issue #2826826 by vasike, dpi, raman.b, rpayanm, jibran, gpap, mpolishchuck,...

Issue #2826826 by vasike, dpi, raman.b, rpayanm, jibran, gpap, mpolishchuck, rwohleb, ranjith_kumar_k_u, smustgrave, johnnydarkko, mrinalini9, Zarpele, Berdir, amateescu, hchonov, amitaibu, larowlan, heddn, RoySegall, quietone: Entity autocomplete widget does not pass along entity to AJAX request
parent 08bde872
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -173,6 +173,12 @@ public static function processEntityAutocomplete(array &$element, FormStateInter
    // Store the selection settings in the key/value store and pass a hashed key
    // in the route parameters.
    $selection_settings = $element['#selection_settings'] ?? [];
    // Don't serialize the entity, it will be added explicitly afterwards.
    if (isset($selection_settings['entity']) && ($selection_settings['entity'] instanceof EntityInterface)) {
      $element['#autocomplete_query_parameters']['entity_type'] = $selection_settings['entity']->getEntityTypeId();
      $element['#autocomplete_query_parameters']['entity_id'] = $selection_settings['entity']->id();
      unset($selection_settings['entity']);
    }
    $data = serialize($selection_settings) . $element['#target_type'] . $element['#selection_handler'];
    $selection_settings_key = Crypt::hmacBase64($data, Settings::getHashSalt());

+5 −0
Original line number Diff line number Diff line
@@ -103,6 +103,11 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
      'match_limit' => $this->getSetting('match_limit'),
    ];

    // Append the entity if it is already created.
    if (!$entity->isNew()) {
      $selection_settings['entity'] = $entity;
    }

    $element += [
      '#type' => 'entity_autocomplete',
      '#target_type' => $this->getFieldSetting('target_type'),
+8 −2
Original line number Diff line number Diff line
@@ -157,7 +157,7 @@ public static function validatePattern(&$element, FormStateInterface $form_state
   *
   * This sets up autocomplete functionality for elements with an
   * #autocomplete_route_name property, using the #autocomplete_route_parameters
   * property if present.
   * and #autocomplete_query_parameters properties if present.
   *
   * For example, suppose your autocomplete route name is
   * 'mymodule.autocomplete' and its path is
@@ -176,6 +176,8 @@ public static function validatePattern(&$element, FormStateInterface $form_state
   *     autocomplete JavaScript library.
   *   - #autocomplete_route_parameters: The parameters to be used in
   *     conjunction with the route name.
   *   - #autocomplete_query_parameters: The parameters to be used in
   *     query string
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   * @param array $complete_form
@@ -190,7 +192,11 @@ public static function processAutocomplete(&$element, FormStateInterface $form_s

    if (!empty($element['#autocomplete_route_name'])) {
      $parameters = $element['#autocomplete_route_parameters'] ?? [];
      $url = Url::fromRoute($element['#autocomplete_route_name'], $parameters)->toString(TRUE);
      $options = [];
      if (!empty($element['#autocomplete_query_parameters'])) {
        $options['query'] = $element['#autocomplete_query_parameters'];
      }
      $url = Url::fromRoute($element['#autocomplete_route_name'], $parameters, $options)->toString(TRUE);
      /** @var \Drupal\Core\Access\AccessManagerInterface $access_manager */
      $access_manager = \Drupal::service('access_manager');
      $access = $access_manager->checkNamedRoute($element['#autocomplete_route_name'], $parameters, \Drupal::currentUser(), TRUE);
+11 −0
Original line number Diff line number Diff line
@@ -99,6 +99,17 @@ public function handleAutocomplete(Request $request, $target_type, $selection_ha
        throw new AccessDeniedHttpException();
      }

      $entity_type_id = $request->query->get('entity_type');
      if ($entity_type_id && $this->entityTypeManager()->hasDefinition($entity_type_id)) {
        $entity_id = $request->query->get('entity_id');
        if ($entity_id) {
          $entity = $this->entityTypeManager()->getStorage($entity_type_id)->load($entity_id);
          if ($entity->access('update')) {
            $selection_settings['entity'] = $entity;
          }
        }
      }

      $matches = $this->matcher->getMatches($target_type, $selection_handler, $selection_settings, $typed_string);
    }

+4 −0
Original line number Diff line number Diff line
# Schema for the entity reference 'entity_test_all_except_host' selection
# handler settings.
entity_reference_selection.entity_test_all_except_host:
  type: entity_reference_selection.default
Loading