You are here

public static function FormTrait::getElementSelector in Markdown 8.2

Retrieves the selector for an element.

Parameters

string $name: The name of the element.

array $parents: An array of parents.

Return value

string The selector for an element.

2 calls to FormTrait::getElementSelector()
FormTrait::addElementState in src/Traits/FormTrait.php
Adds a #states selector to an element.
FormTrait::resetToDefault in src/Traits/FormTrait.php
Allows a form element to be reset to its default value.

File

src/Traits/FormTrait.php, line 144

Class

FormTrait
Trait providing helpful methods when dealing with forms.

Namespace

Drupal\markdown\Traits

Code

public static function getElementSelector($name, array $parents) {

  // Immediately return if name is already an input selector.
  if (strpos($name, ':input[name="') === 0) {
    return $name;
  }

  // Add the name of the element that will be used for the condition.
  $parents[] = $name;

  // Remove the first parent as the base selector.
  $selector = array_shift($parents);

  // Join remaining parents with [].
  if ($parents) {
    $selector .= '[' . implode('][', $parents) . ']';
  }
  return $selector ? ':input[name="' . $selector . '"]' : '';
}