You are here

public function YamlFormElementBase::form in YAML Form 8

Gets the actual configuration form array to be built.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array An associative array contain the element's configuration form without any default values..

Overrides YamlFormElementInterface::form

18 calls to YamlFormElementBase::form()
Captcha::form in src/Plugin/YamlFormElement/Captcha.php
Gets the actual configuration form array to be built.
Color::form in src/Plugin/YamlFormElement/Color.php
Gets the actual configuration form array to be built.
DateBase::form in src/Plugin/YamlFormElement/DateBase.php
Gets the actual configuration form array to be built.
NumericBase::form in src/Plugin/YamlFormElement/NumericBase.php
Gets the actual configuration form array to be built.
OptionsBase::form in src/Plugin/YamlFormElement/OptionsBase.php
Gets the actual configuration form array to be built.

... See full list

18 methods override YamlFormElementBase::form()
Captcha::form in src/Plugin/YamlFormElement/Captcha.php
Gets the actual configuration form array to be built.
Color::form in src/Plugin/YamlFormElement/Color.php
Gets the actual configuration form array to be built.
DateBase::form in src/Plugin/YamlFormElement/DateBase.php
Gets the actual configuration form array to be built.
NumericBase::form in src/Plugin/YamlFormElement/NumericBase.php
Gets the actual configuration form array to be built.
OptionsBase::form in src/Plugin/YamlFormElement/OptionsBase.php
Gets the actual configuration form array to be built.

... See full list

File

src/YamlFormElementBase.php, line 916

Class

YamlFormElementBase
Provides a base class for a form element.

Namespace

Drupal\yamlform

Code

public function form(array $form, FormStateInterface $form_state) {

  /** @var \Drupal\yamlform_ui\Form\YamlFormUiElementFormInterface $form_object */
  $form_object = $form_state
    ->getFormObject();
  $yamlform = $form_object
    ->getYamlForm();

  /* Element settings */
  $form['element'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Element settings'),
    '#access' => TRUE,
    '#weight' => -50,
  ];
  $form['element']['title'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Title'),
    '#description' => $this
      ->t('This is used as a descriptive label when displaying this form element.'),
    '#required' => TRUE,
    '#attributes' => [
      'autofocus' => 'autofocus',
    ],
  ];
  $form['element']['description'] = [
    '#type' => 'yamlform_html_editor',
    '#title' => $this
      ->t('Description'),
    '#description' => $this
      ->t('A short description of the element used as help for the user when he/she uses the form.'),
  ];
  if ($this
    ->isComposite()) {
    $form['element']['default_value'] = [
      '#type' => 'yamlform_codemirror',
      '#mode' => 'yaml',
      '#title' => $this
        ->t('Default value'),
      '#description' => $this
        ->t('The default value of the form element.'),
    ];
  }
  else {
    $form['element']['default_value'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Default value'),
      '#description' => $this
        ->t('The default value of the form element.'),
    ];
  }
  $form['element']['value'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Value'),
    '#description' => $this
      ->t('The value of the form element.'),
  ];

  /* Form display */
  $form['form'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Form display'),
  ];
  $form['form']['title_display'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Title display'),
    '#options' => [
      '' => '',
      'before' => $this
        ->t('Before'),
      'after' => $this
        ->t('After'),
      'inline' => $this
        ->t('Inline'),
      'invisible' => $this
        ->t('Invisible'),
      'attribute' => $this
        ->t('Attribute'),
    ],
    '#description' => $this
      ->t('Determines the placement of the title.'),
  ];
  $form['form']['description_display'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Description display'),
    '#options' => [
      '' => '',
      'before' => $this
        ->t('Before'),
      'after' => $this
        ->t('After'),
      'invisible' => $this
        ->t('Invisible'),
      'tooltip' => $this
        ->t('Tooltip'),
    ],
    '#description' => $this
      ->t('Determines the placement of the description.'),
  ];
  $form['form']['field_prefix'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Field prefix'),
    '#description' => $this
      ->t('Text or code that is placed directly in front of the input. This can be used to prefix an input with a constant string. Examples: $, #, -.'),
    '#size' => 10,
  ];
  $form['form']['field_suffix'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Field suffix'),
    '#description' => $this
      ->t('Text or code that is placed directly after the input. This can be used to add a unit to an input. Examples: lb, kg, %.'),
    '#size' => 10,
  ];
  $form['form']['size'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Size'),
    '#description' => $this
      ->t('Leaving blank will use the default size.'),
    '#min' => 1,
    '#size' => 4,
  ];
  $form['form']['maxlength'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Maxlength'),
    '#description' => $this
      ->t('Leaving blank will use the default maxlength.'),
    '#min' => 1,
    '#size' => 4,
  ];
  $form['form']['rows'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Rows'),
    '#description' => $this
      ->t('Leaving blank will use the default rows.'),
    '#min' => 1,
    '#size' => 4,
  ];
  $form['form']['placeholder'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Placeholder'),
    '#description' => $this
      ->t('The placeholder will be shown in the element until the user starts entering a value.'),
  ];
  $form['form']['open'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Open'),
    '#description' => $this
      ->t('Contents should be visible (open) to the user.'),
    '#return_value' => TRUE,
  ];

  /* Flexbox item */
  $form['flex'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Flexbox item'),
    '#description' => $this
      ->t('Learn more about using <a href=":href">flexbox layouts</a>.', [
      ':href' => 'http://www.w3schools.com/css/css3_flexbox.asp',
    ]),
  ];
  $flex_range = range(0, 12);
  $form['flex']['flex'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Flex'),
    '#description' => $this
      ->t('The flex property specifies the length of the item, relative to the rest of the flexible items inside the same container.') . '<br/>' . $this
      ->t('Defaults to: %value', [
      '%value' => 1,
    ]),
    '#options' => [
      0 => $this
        ->t('0 (none)'),
    ] + array_combine($flex_range, $flex_range),
  ];

  /* Wrapper and element attributes */
  $form['wrapper_attributes'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Wrapper attributes'),
    '#open' => TRUE,
  ];
  $form['wrapper_attributes']['wrapper_attributes'] = [
    '#type' => 'yamlform_element_attributes',
    '#title' => $this
      ->t('Wrapper'),
    '#class__description' => $this
      ->t("Apply classes to the element's wrapper around both the field and its label. Select 'custom...' to enter custom classes."),
    '#style__description' => $this
      ->t("Apply custom styles to the element's wrapper around both the field and its label."),
    '#attributes__description' => $this
      ->t("Enter additional attributes to be added the element's wrapper."),
    '#classes' => $this->configFactory
      ->get('yamlform.settings')
      ->get('elements.wrapper_classes'),
  ];
  $form['element_attributes'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Element attributes'),
    '#open' => TRUE,
  ];
  $form['element_attributes']['attributes'] = [
    '#type' => 'yamlform_element_attributes',
    '#title' => $this
      ->t('Element'),
    '#classes' => $this->configFactory
      ->get('yamlform.settings')
      ->get('elements.classes'),
  ];

  /* Validation */

  // Placeholder form elements with #options.
  // @see \Drupal\yamlform\Plugin\YamlFormElement\OptionsBase::form
  $form['options'] = [];
  $form['options_other'] = [];
  $form['validation'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Form validation'),
  ];
  $form['validation']['required'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Required'),
    '#description' => $this
      ->t('Check this option if the user must enter a value.'),
    '#return_value' => TRUE,
  ];
  $form['validation']['required_error'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Custom required error message'),
    '#description' => $this
      ->t('If set, this message will be used when a required form element is empty, instead of the default "Field x is required." message.'),
    '#states' => [
      'visible' => [
        ':input[name="properties[required]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['validation']['unique'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Unique'),
    '#description' => $this
      ->t('Check that all entered values for this element are unique. The same value is not allowed to be used twice.'),
    '#return_value' => TRUE,
  ];

  /* Conditional logic */
  $form['conditional'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Conditional logic'),
  ];
  $form['conditional']['states'] = [
    '#type' => 'yamlform_element_states',
    '#state_options' => $this
      ->getElementStateOptions(),
    '#selector_options' => $yamlform
      ->getElementsSelectorOptions(),
  ];

  /* Submission display */
  $form['display'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Submission display'),
  ];
  $form['display']['format'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Format'),
    '#options' => $this
      ->getFormats(),
  ];

  /* Element access */
  $operations = [
    'create' => [
      '#title' => $this
        ->t('Create form submission'),
      '#description' => $this
        ->t('Select roles and users that should be able to populate this element when creating a new submission.'),
    ],
    'update' => [
      '#title' => $this
        ->t('Update form submission'),
      '#description' => $this
        ->t('Select roles and users that should be able to update this element when updating an existing submission.'),
    ],
    'view' => [
      '#title' => $this
        ->t('View form submission'),
      '#description' => $this
        ->t('Select roles and users that should be able to view this element when viewing a submission.'),
    ],
  ];
  $form['access'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Element access'),
  ];
  if (!$this->currentUser
    ->hasPermission('administer yamlform') && !$this->currentUser
    ->hasPermission('administer yamlform element access')) {
    $form['access'] = FALSE;
  }
  foreach ($operations as $operation => $operation_element) {
    $form['access']['access_' . $operation] = $operation_element + [
      '#type' => 'details',
    ];
    $form['access']['access_' . $operation]['access_' . $operation . '_roles'] = [
      '#type' => 'yamlform_roles',
      '#title' => $this
        ->t('Roles'),
    ];
    $form['access']['access_' . $operation]['access_' . $operation . '_users'] = [
      '#type' => 'yamlform_users',
      '#title' => $this
        ->t('Users'),
    ];
  }

  /* Administration */
  $form['admin'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Administration'),
  ];
  $form['admin']['private'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Private'),
    '#description' => $this
      ->t('Private elements are shown only to users with results access.'),
    '#weight' => 50,
    '#return_value' => TRUE,
  ];
  $form['admin']['admin_title'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Admin title'),
    '#description' => $this
      ->t('The admin title will be displayed when managing elements and viewing & downloading submissions.'),
  ];
  return $form;
}