View source
<?php
namespace Drupal\markdown\Form;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Form\FormStateInterface;
use Drupal\markdown\BcSupport\SubformState as CoreSubformState;
use Drupal\markdown\Traits\FormTrait;
class SubformState extends CoreSubformState implements SubformStateInterface {
use FormTrait {
addElementState as traitAddElementState;
}
protected function __construct(array &$subform, array &$parent_form, FormStateInterface $parent_form_state) {
$this->decoratedFormState = $parent_form_state;
$this->parentForm =& $parent_form;
$this->subform =& $subform;
}
public function conditionalElement(array $element, $state, $name, array $conditions) {
$element += [
'#type' => 'html_tag',
];
if ($element['#type'] === 'container') {
$element += [
'#theme_wrappers' => [
'container__markdown_conditional_element__' . $name,
],
];
}
if ($element['#type'] === 'html_tag') {
$element += [
'#tag' => 'span',
];
}
$element['#attributes']['class'][] = 'js-form-item';
$this
->addElementState($element, $state, $name, $conditions);
$element['#attached']['library'][] = 'core/drupal.states';
$element['#attributes']['data-drupal-states'] = Json::encode($element['#states']);
return drupal_render($element);
}
public static function createForSubform(array &$subform, array &$parent_form, FormStateInterface $parent_form_state) {
if (!isset($subform['#parents']) && $parent_form_state instanceof SubformStateInterface && ($name = array_search($subform, $parent_form, TRUE))) {
$subform['#parents'] = array_merge($parent_form_state
->getAllParents(), [
$name,
]);
}
return parent::createForSubform($subform, $parent_form, $parent_form_state);
}
public function addElementState(array &$element, $state, $name, array $conditions, array $parents = NULL) {
if (!isset($parents)) {
$parents = $this
->getAllParents();
}
static::traitAddElementState($element, $state, $name, $conditions, $parents);
}
public function createParents($name = NULL, $property = '#parents') {
$parents = $this
->getAllParents($property);
if ($name) {
$parents = array_merge($parents, (array) $name);
}
return $parents;
}
public function getAllParents($property = '#parents') {
if (!isset($this->parentForm[$property])) {
throw new \RuntimeException(sprintf('The subform and parent form must contain the %s property, which must be an array. Try calling this method from a #process callback instead.', $property));
}
return array_merge($this->parentForm[$property], $this
->getParents($property));
}
public function &getParentForm() {
return $this->parentForm;
}
}