You are here

protected function EntityFormWizardBase::customizeForm in Chaos Tool Suite (ctools) 8.3

Helper function for generating label and id form elements.

Parameters

array $form:

\Drupal\Core\Form\FormStateInterface $form_state:

Return value

array

Overrides FormWizardBase::customizeForm

File

src/Wizard/EntityFormWizardBase.php, line 128

Class

EntityFormWizardBase
The base class for all entity form wizards.

Namespace

Drupal\ctools\Wizard

Code

protected function customizeForm(array $form, FormStateInterface $form_state) {
  $form = parent::customizeForm($form, $form_state);
  if ($this->machine_name) {
    $entity = $this->entityTypeManager
      ->getStorage($this
      ->getEntityType())
      ->load($this->machine_name);
  }
  else {
    $entity = NULL;
  }
  $cached_values = $form_state
    ->getTemporaryValue('wizard');

  // If the entity already exists, allow for non-linear step interaction.
  if ($entity) {

    // Setup the step rendering theme element.
    $prefix = [
      '#theme' => [
        'ctools_wizard_trail_links',
      ],
      '#wizard' => $this,
      '#cached_values' => $cached_values,
    ];
    $form['#prefix'] = \Drupal::service('renderer')
      ->render($prefix);
  }

  // Get the current form operation.
  $operation = $this
    ->getOperation($cached_values);
  $operations = $this
    ->getOperations($cached_values);
  $default_operation = reset($operations);
  if ($operation['form'] == $default_operation['form']) {

    // Get the plugin definition of this entity.
    $definition = $this->entityTypeManager
      ->getDefinition($this
      ->getEntityType());

    // Create id and label form elements.
    $form['name'] = [
      '#type' => 'fieldset',
      '#attributes' => [
        'class' => [
          'fieldset-no-legend',
        ],
      ],
      '#title' => $this
        ->getWizardLabel(),
    ];
    $form['name']['label'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->getMachineLabel(),
      '#required' => TRUE,
      '#size' => 32,
      '#default_value' => !empty($cached_values['label']) ? $cached_values['label'] : '',
      '#maxlength' => 255,
      '#disabled' => !empty($cached_values['label']),
    ];
    $form['name']['id'] = [
      '#type' => 'machine_name',
      '#maxlength' => 128,
      '#machine_name' => [
        'source' => [
          'name',
          'label',
        ],
        'exists' => $this
          ->exists(),
      ],
      '#description' => $this
        ->t('A unique machine-readable name for this @entity_type. It must only contain lowercase letters, numbers, and underscores.', [
        '@entity_type' => $definition
          ->getLabel(),
      ]),
      '#default_value' => !empty($cached_values['id']) ? $cached_values['id'] : '',
      '#disabled' => !empty($cached_values['id']),
    ];
  }
  return $form;
}