You are here

abstract class YamlFormUiElementFormBase in YAML Form 8

Provides a base class for form element forms.

The basic workflow for handling form elements.

  • Read the element.
  • Build element's properties form.
  • Set the property values.
  • Alter the element's properties form.
  • Process the element's properties form.
  • Validate the element's properties form.
  • Submit the element's properties form.
  • Get property values from the form state's values.
  • Remove default properties from the element's properties.
  • Update element properties.

Hierarchy

Expanded class hierarchy of YamlFormUiElementFormBase

File

modules/yamlform_ui/src/Form/YamlFormUiElementFormBase.php, line 32

Namespace

Drupal\yamlform_ui\Form
View source
abstract class YamlFormUiElementFormBase extends FormBase implements YamlFormUiElementFormInterface {
  use YamlFormDialogTrait;

  /**
   * The renderer.
   *
   * @var \Drupal\Core\Render\RendererInterface
   */
  protected $renderer;

  /**
   * Form element manager.
   *
   * @var \Drupal\yamlform\YamlFormElementManagerInterface
   */
  protected $elementManager;

  /**
   * Form element validator.
   *
   * @var \Drupal\yamlform\YamlFormEntityElementsValidator
   */
  protected $elementsValidator;

  /**
   * The form.
   *
   * @var \Drupal\yamlform\YamlFormInterface
   */
  protected $yamlform;

  /**
   * The form element.
   *
   * @var array
   */
  protected $element = [];

  /**
   * The form element's original element type.
   *
   * @var string
   */
  protected $originalType;

  /**
   * The action of the current form.
   *
   * @var string
   */
  protected $action;

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'yamlform_ui_element_form';
  }

  /**
   * Constructs a new YamlFormUiElementFormBase.
   *
   * @param \Drupal\Core\Render\RendererInterface $renderer
   *   The renderer.
   * @param \Drupal\yamlform\YamlFormElementManagerInterface $element_manager
   *   The form element manager.
   * @param \Drupal\yamlform\YamlFormEntityElementsValidator $elements_validator
   *   Form element validator.
   */
  public function __construct(RendererInterface $renderer, YamlFormElementManagerInterface $element_manager, YamlFormEntityElementsValidator $elements_validator) {
    $this->renderer = $renderer;
    $this->elementManager = $element_manager;
    $this->elementsValidator = $elements_validator;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('renderer'), $container
      ->get('plugin.manager.yamlform.element'), $container
      ->get('yamlform.elements_validator'));
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, YamlFormInterface $yamlform = NULL, $key = NULL, $parent_key = '') {
    $this->yamlform = $yamlform;
    $yamlform_element = $this
      ->getYamlFormElement();
    $form['properties'] = $yamlform_element
      ->buildConfigurationForm([], $form_state);

    // Move messages to the top of the form.
    if (isset($form['properties']['messages'])) {
      $form['messages'] = $form['properties']['messages'];
      $form['messages']['#weight'] = -100;
      unset($form['properties']['messages']);
    }

    // Set parent key.
    $form['parent_key'] = [
      '#type' => 'value',
      '#value' => $parent_key,
    ];

    // Set element type.
    $form['properties']['element']['type'] = [
      '#type' => 'item',
      '#title' => $this
        ->t('Type'),
      'label' => [
        '#markup' => $yamlform_element
          ->getPluginLabel(),
      ],
      '#weight' => -100,
      '#parents' => [
        'type',
      ],
    ];

    // Set change element type.
    if ($key && $yamlform_element
      ->getRelatedTypes($this->element)) {
      $route_parameters = [
        'yamlform' => $yamlform
          ->id(),
        'key' => $key,
      ];
      if ($this->originalType) {
        $original_yamlform_element = $this->elementManager
          ->createInstance($this->originalType);
        $route_parameters = [
          'yamlform' => $yamlform
            ->id(),
          'key' => $key,
        ];
        $form['properties']['element']['type']['cancel'] = [
          '#type' => 'link',
          '#title' => $this
            ->t('Cancel'),
          '#url' => new Url('entity.yamlform_ui.element.edit_form', $route_parameters),
          '#attributes' => YamlFormDialogHelper::getModalDialogAttributes(800, [
            'button',
            'button--small',
          ]),
        ];
        $form['properties']['element']['type']['#description'] = '(' . $this
          ->t('Changing from %type', [
          '%type' => $original_yamlform_element
            ->getPluginLabel(),
        ]) . ')';
      }
      else {
        $form['properties']['element']['type']['change_type'] = [
          '#type' => 'link',
          '#title' => $this
            ->t('Change'),
          '#url' => new Url('entity.yamlform_ui.change_element', $route_parameters),
          '#attributes' => YamlFormDialogHelper::getModalDialogAttributes(800, [
            'button',
            'button--small',
          ]),
        ];
      }
    }

    // Set element key reserved word warning message.
    if (!$key) {
      $reserved_keys = [
        'form_build_id',
        'form_token',
        'form_id',
        'data',
        'op',
      ];
      $reserved_keys = array_merge($reserved_keys, array_keys(\Drupal::service('entity_field.manager')
        ->getBaseFieldDefinitions('yamlform_submission')));
      $form['#attached']['drupalSettings']['yamlform_ui']['reserved_keys'] = $reserved_keys;
      $form['#attached']['library'][] = 'yamlform_ui/yamlform_ui.element';
      $form['properties']['element']['key_warning'] = [
        '#type' => 'yamlform_message',
        '#message_type' => 'warning',
        '#message_message' => $this
          ->t("Please avoid using the reserved word '@key' as the element's key."),
        '#weight' => -99,
        '#attributes' => [
          'style' => 'display:none',
        ],
      ];
    }

    // Set element key.
    $form['properties']['element']['key'] = [
      '#type' => 'machine_name',
      '#title' => $this
        ->t('Key'),
      '#machine_name' => [
        'label' => $this
          ->t('Key'),
        'exists' => [
          $this,
          'exists',
        ],
        'source' => [
          'title',
        ],
      ],
      '#required' => TRUE,
      '#parents' => [
        'key',
      ],
      '#disabled' => $key ? TRUE : FALSE,
      '#default_value' => $key,
      '#weight' => -98,
    ];

    // Remove the key's help text (aka description) once it has been set.
    if ($key) {
      $form['properties']['element']['key']['#description'] = NULL;
    }

    // Use title for key (machine_name).
    if (isset($form['properties']['element']['title'])) {
      $form['properties']['element']['key']['#machine_name']['source'] = [
        'properties',
        'element',
        'title',
      ];
      $form['properties']['element']['title']['#id'] = 'title';
    }

    // Set flex.
    // Hide #flex property if parent element is not a 'yamlform_flexbox'.
    if (isset($form['properties']['flex']) && !$this
      ->isParentElementFlexbox($key, $parent_key)) {
      $form['properties']['flex']['#access'] = FALSE;
    }

    // Set actions.
    $form['actions'] = [
      '#type' => 'actions',
    ];
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Save'),
      '#button_type' => 'primary',
      '#_validate_form' => TRUE,
    ];
    $form = $this
      ->buildDialog($form, $form_state);
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {

    // Only validate the submit button.
    $button = $form_state
      ->getTriggeringElement();
    if (empty($button['#_validate_form'])) {
      return;
    }

    // The form element configuration is stored in the 'properties' key in
    // the form, pass that through for validation.
    $element_form_state = clone $form_state;
    $element_form_state
      ->setValues($form_state
      ->getValue('properties'));

    // Validate configuration form.
    $yamlform_element = $this
      ->getYamlFormElement();
    $yamlform_element
      ->validateConfigurationForm($form, $element_form_state);

    // Get errors for element validation.
    $element_errors = $element_form_state
      ->getErrors();
    foreach ($element_errors as $element_error) {
      $form_state
        ->setErrorByName(NULL, $element_error);
    }

    // Stop validation is the element properties has any errors.
    if ($form_state
      ->hasAnyErrors()) {
      return;
    }

    // Set element properties.
    $properties = $yamlform_element
      ->getConfigurationFormProperties($form, $element_form_state);
    $parent_key = $form_state
      ->getValue('parent_key');
    $key = $form_state
      ->getValue('key');
    if ($key) {
      $this->yamlform
        ->setElementProperties($key, $properties, $parent_key);

      // Validate elements.
      if ($messages = $this->elementsValidator
        ->validate($this->yamlform)) {
        $t_args = [
          ':href' => Url::fromRoute('entity.yamlform.source_form', [
            'yamlform' => $this->yamlform
              ->id(),
          ])
            ->toString(),
        ];
        $form_state
          ->setErrorByName('elements', $this
          ->t('There has been error validating the elements. You may need to edit the <a href=":href">YAML source</a> to resolve the issue.', $t_args));
        foreach ($messages as $message) {
          drupal_set_message($message, 'error');
        }
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $yamlform_element = $this
      ->getYamlFormElement();
    if ($response = $this
      ->validateDialog($form, $form_state)) {
      return $response;
    }

    // The form element configuration is stored in the 'properties' key in
    // the form, pass that through for submission.
    $element_form_state = clone $form_state;
    $element_form_state
      ->setValues($form_state
      ->getValue('properties'));

    // Submit element configuration.
    // Generally, elements will not be processing any submitted properties.
    // It is possible that a custom element might need to call a third-party API
    // to 'register' the element.
    $yamlform_element
      ->submitConfigurationForm($form, $element_form_state);

    // Save the form with its updated element.
    $this->yamlform
      ->save();

    // Display status message.
    $properties = $form_state
      ->getValue('properties');
    $t_args = [
      '%title' => !empty($properties['title']) ? $properties['title'] : $form_state
        ->getValue('key'),
      '@action' => $this->action,
    ];
    drupal_set_message($this
      ->t('%title has been @action.', $t_args));

    // Redirect.
    return $this
      ->redirectForm($form, $form_state, $this->yamlform
      ->toUrl('edit-form'));
  }

  /**
   * Determines if the form element key already exists.
   *
   * @param string $key
   *   The form element key.
   *
   * @return bool
   *   TRUE if the form element key, FALSE otherwise.
   */
  public function exists($key) {
    $elements = $this->yamlform
      ->getElementsInitializedAndFlattened();
    return isset($elements[$key]) ? TRUE : FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function isNew() {
    return $this instanceof YamlFormUiElementAddForm ? TRUE : FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function getYamlForm() {
    return $this->yamlform;
  }

  /**
   * {@inheritdoc}
   */
  public function getYamlFormElement() {
    return $this->elementManager
      ->getElementInstance($this->element);
  }

  /**
   * Determine if the parent element is a 'yamlform_flexbox'.
   *
   * @param string|null $key
   *   The element's key. Only applicable for existing elements.
   * @param string|null $parent_key
   *   The element's parent key. Only applicable for new elements.
   *   Parent key is set via query string parameter. (?parent={parent_key})
   *
   * @return bool
   *   TRUE if the parent element is a 'yamlform_flexbox'.
   */
  protected function isParentElementFlexbox($key = NULL, $parent_key = NULL) {
    $elements = $this->yamlform
      ->getElementsInitializedAndFlattened();

    // Check the element #yamlform_parent_flexbox property.
    if ($key && isset($elements[$key])) {
      return $elements[$key]['#yamlform_parent_flexbox'];
    }

    // Check the parent element #type.
    if ($parent_key && isset($elements[$parent_key]) && isset($elements[$parent_key]['#type'])) {
      return $elements[$parent_key]['#type'] == 'yamlform_flexbox' ? TRUE : FALSE;
    }
    return FALSE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.
YamlFormDialogTrait::buildDialog protected function Add modal dialog support to a form.
YamlFormDialogTrait::isModalDialog protected function Is the current request for an AJAX modal dialog.
YamlFormDialogTrait::redirectForm protected function Handler dialog redirect after form is submitted.
YamlFormDialogTrait::validateDialog protected function Display validation error messages in modal dialog.
YamlFormUiElementFormBase::$action protected property The action of the current form.
YamlFormUiElementFormBase::$element protected property The form element.
YamlFormUiElementFormBase::$elementManager protected property Form element manager.
YamlFormUiElementFormBase::$elementsValidator protected property Form element validator.
YamlFormUiElementFormBase::$originalType protected property The form element's original element type.
YamlFormUiElementFormBase::$renderer protected property The renderer.
YamlFormUiElementFormBase::$yamlform protected property The form.
YamlFormUiElementFormBase::buildForm public function Form constructor. Overrides FormInterface::buildForm 4
YamlFormUiElementFormBase::create public static function Instantiates a new instance of this class. Overrides FormBase::create
YamlFormUiElementFormBase::exists public function Determines if the form element key already exists. 1
YamlFormUiElementFormBase::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId 1
YamlFormUiElementFormBase::getYamlForm public function Return the form associated with this form. Overrides YamlFormUiElementFormInterface::getYamlForm
YamlFormUiElementFormBase::getYamlFormElement public function Return the form element associated with this form. Overrides YamlFormUiElementFormInterface::getYamlFormElement
YamlFormUiElementFormBase::isNew public function Is new element. Overrides YamlFormUiElementFormInterface::isNew
YamlFormUiElementFormBase::isParentElementFlexbox protected function Determine if the parent element is a 'yamlform_flexbox'.
YamlFormUiElementFormBase::submitForm public function Form submission handler. Overrides FormInterface::submitForm 1
YamlFormUiElementFormBase::validateForm public function Form validation handler. Overrides FormBase::validateForm
YamlFormUiElementFormBase::__construct public function Constructs a new YamlFormUiElementFormBase.