Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Form.php

function Form::initialize

Adds form elements related to this form.

Creates an internal copy of the submitted 'button' element and the form node or the entire document depending on whether we need to find non-descendant elements through HTML5 'form' attribute.

1 call to Form::initialize()
Form::__construct in vendor/symfony/dom-crawler/Form.php

File

vendor/symfony/dom-crawler/Form.php, line 390

Class

Form
Form represents an HTML form.

Namespace

Symfony\Component\DomCrawler

Code

private function initialize() : void {
    $this->fields = new FormFieldRegistry();
    $xpath = new \DOMXPath($this->node->ownerDocument);
    // add submitted button if it has a valid name
    if ('form' !== $this->button->nodeName && $this->button
        ->hasAttribute('name') && $this->button
        ->getAttribute('name')) {
        if ('input' == $this->button->nodeName && 'image' == strtolower($this->button
            ->getAttribute('type'))) {
            $name = $this->button
                ->getAttribute('name');
            $this->button
                ->setAttribute('value', '0');
            // temporarily change the name of the input node for the x coordinate
            $this->button
                ->setAttribute('name', $name . '.x');
            $this->set(new Field\InputFormField($this->button));
            // temporarily change the name of the input node for the y coordinate
            $this->button
                ->setAttribute('name', $name . '.y');
            $this->set(new Field\InputFormField($this->button));
            // restore the original name of the input node
            $this->button
                ->setAttribute('name', $name);
        }
        else {
            $this->set(new Field\InputFormField($this->button));
        }
    }
    // find form elements corresponding to the current form
    if ($this->node
        ->hasAttribute('id')) {
        // corresponding elements are either descendants or have a matching HTML5 form attribute
        $formId = Crawler::xpathLiteral($this->node
            ->getAttribute('id'));
        $fieldNodes = $xpath->query(\sprintf('( descendant::input[@form=%s] | descendant::button[@form=%1$s] | descendant::textarea[@form=%1$s] | descendant::select[@form=%1$s] | //form[@id=%1$s]//input[not(@form)] | //form[@id=%1$s]//button[not(@form)] | //form[@id=%1$s]//textarea[not(@form)] | //form[@id=%1$s]//select[not(@form)] )[( not(ancestor::template) or ancestor::turbo-stream )]', $formId));
    }
    else {
        // do the xpath query with $this->node as the context node, to only find descendant elements
        // however, descendant elements with form attribute are not part of this form
        $fieldNodes = $xpath->query('( descendant::input[not(@form)] | descendant::button[not(@form)] | descendant::textarea[not(@form)] | descendant::select[not(@form)] )[( not(ancestor::template) or ancestor::turbo-stream )]', $this->node);
    }
    foreach ($fieldNodes as $node) {
        $this->addField($node);
    }
    if ($this->baseHref && '' !== $this->node
        ->getAttribute('action')) {
        $this->currentUri = $this->baseHref;
    }
}
RSS feed
Powered by Drupal