preRenderAjaxForm example

$this->container->set('url_generator', $url_generator);

    $element = [
      '#type' => 'select',
      '#id' => 'test',
      '#ajax' => [
        'wrapper' => 'foo',
        'callback' => 'test-callback',
      ],
    ];

    $element = RenderElement::preRenderAjaxForm($element);

    $this->assertTrue($element['#ajax_processed']);
    $this->assertEquals($url$element['#attached']['drupalSettings']['ajax']['test']['url']);
  }

  /** * @covers ::preRenderAjaxForm */
  public function testPreRenderAjaxFormWithQueryOptions() {
    $request = Request::create('/test');
    $request->query->set('foo', 'bar');
    

    elseif (isset($element['#id'])) {
      $element['#options']['attributes']['id'] = $element['#id'];
    }

    // Conditionally invoke self::preRenderAjaxForm(), if #ajax is set.     if (isset($element['#ajax']) && !isset($element['#ajax_processed'])) {
      // If no HTML ID was found above, automatically create one.       if (!isset($element['#id'])) {
        $element['#id'] = $element['#options']['attributes']['id'] = HtmlUtility::getUniqueId('ajax-link');
      }
      $element = static::preRenderAjaxForm($element);
    }

    if (!empty($element['#url']) && $element['#url'] instanceof CoreUrl) {
      $options = NestedArray::mergeDeep($element['#url']->getOptions()$element['#options']);
      /** @var \Drupal\Core\Utility\LinkGenerator $link_generator */
      $link_generator = \Drupal::service('link_generator');
      $generated_link = $link_generator->generate($element['#title']$element['#url']->setOptions($options));
      $element['#markup'] = $generated_link;
      $generated_link->merge(BubbleableMetadata::createFromRenderArray($element))
        ->applyTo($element);
    }
    

  public static function processAjaxForm(&$element, FormStateInterface $form_state, &$complete_form) {
    return static::preRenderAjaxForm($element);
  }

  /** * Adds Ajax information about an element to communicate with JavaScript. * * If #ajax is set on an element, this additional JavaScript is added to the * page header to attach the Ajax behaviors. See ajax.js for more information. * * @param array $element * An associative array containing the properties of the element. * Properties used: * - #ajax['event'] * - #ajax['prevent'] * - #ajax['url'] * - #ajax['httpMethod'] * - #ajax['callback'] * - #ajax['options'] * - #ajax['wrapper'] * - #ajax['parameters'] * - #ajax['effect'] * - #ajax['accepts'] * * @return array * The processed element with the necessary JavaScript attached to it. */
Home | Imprint | This part of the site doesn't use cookies.