You are here

public function YamlFormUiEntityForm::editForm in YAML Form 8

Edit form element's source code form.

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 The form structure.

Overrides YamlFormEntityForm::editForm

File

modules/yamlform_ui/src/YamlFormUiEntityForm.php, line 21

Class

YamlFormUiEntityForm
Base for controller for form UI.

Namespace

Drupal\yamlform_ui

Code

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

  /** @var \Drupal\yamlform\YamlFormInterface $yamlform */
  $yamlform = $this
    ->getEntity();
  if ($yamlform
    ->isNew()) {
    return $form;
  }
  $dialog_attributes = YamlFormDialogHelper::getModalDialogAttributes(800);

  // Build table header.
  $header = [];
  $header['title'] = $this
    ->t('Title');
  $header['add'] = [
    'data' => '',
    'class' => [
      RESPONSIVE_PRIORITY_MEDIUM,
      'yamlform-ui-element-operations',
    ],
  ];
  $header['key'] = [
    'data' => $this
      ->t('Key'),
    'class' => [
      RESPONSIVE_PRIORITY_LOW,
    ],
  ];
  $header['type'] = [
    'data' => $this
      ->t('Type'),
    'class' => [
      RESPONSIVE_PRIORITY_LOW,
    ],
  ];
  if ($yamlform
    ->hasFlexboxLayout()) {
    $header['flex'] = [
      'data' => $this
        ->t('Flex'),
      'class' => [
        RESPONSIVE_PRIORITY_LOW,
      ],
    ];
  }
  $header['required'] = [
    'data' => $this
      ->t('Required'),
    'class' => [
      'yamlform-ui-element-required',
      RESPONSIVE_PRIORITY_LOW,
    ],
  ];
  $header['weight'] = $this
    ->t('Weight');
  $header['parent'] = $this
    ->t('Parent');
  if (!$yamlform
    ->isNew()) {
    $header['operations'] = [
      'data' => $this
        ->t('Operations'),
      'class' => [
        'yamlform-ui-element-operations',
      ],
    ];
  }

  // Build table rows for elements.
  $rows = [];
  $elements = $this
    ->getOrderableElements();
  $delta = count($elements);
  foreach ($elements as $element) {
    $key = $element['#yamlform_key'];
    $plugin_id = $this->elementManager
      ->getElementPluginId($element);

    /** @var \Drupal\yamlform\YamlFormElementInterface $yamlform_element */
    $yamlform_element = $this->elementManager
      ->createInstance($plugin_id);
    $is_container = $yamlform_element
      ->isContainer($element);
    $is_root = $yamlform_element
      ->isRoot();

    // If disabled, display warning.
    if ($yamlform_element
      ->isDisabled()) {
      $yamlform_element
        ->displayDisabledWarning($element);
    }

    // Get row class names.
    $row_class = [
      'draggable',
    ];
    if ($is_root) {
      $row_class[] = 'tabledrag-root';
      $row_class[] = 'yamlform-ui-element-root';
    }
    if (!$is_container) {
      $row_class[] = 'tabledrag-leaf';
    }
    if ($is_container) {
      $row_class[] = 'yamlform-ui-element-container';
    }
    if (!empty($element['#type'])) {
      $row_class[] = 'yamlform-ui-element-type-' . $element['#type'];
    }
    $row_class[] = 'yamlform-ui-element-container';
    $rows[$key]['#attributes']['class'] = $row_class;
    $indentation = NULL;
    if ($element['#yamlform_depth']) {
      $indentation = [
        '#theme' => 'indentation',
        '#size' => $element['#yamlform_depth'],
      ];
    }
    $rows[$key]['title'] = [
      '#markup' => $element['#admin_title'] ?: $element['#title'],
      '#prefix' => !empty($indentation) ? $this->renderer
        ->render($indentation) : '',
    ];
    if ($is_container) {
      $route_parameters = [
        'yamlform' => $yamlform
          ->id(),
      ];
      $route_options = [
        'query' => [
          'parent' => $key,
        ],
      ];
      $rows[$key]['add'] = [
        '#type' => 'link',
        '#title' => $this
          ->t('Add element'),
        '#url' => new Url('entity.yamlform_ui.element', $route_parameters, $route_options),
        '#attributes' => YamlFormDialogHelper::getModalDialogAttributes(800, [
          'button',
          'button-action',
          'button--primary',
          'button--small',
        ]),
      ];
    }
    else {
      $rows[$key]['add'] = [
        '#markup' => '',
      ];
    }
    $rows[$key]['name'] = [
      '#markup' => $element['#yamlform_key'],
    ];
    $rows[$key]['type'] = [
      '#markup' => $yamlform_element
        ->getPluginLabel(),
    ];
    if ($yamlform
      ->hasFlexboxLayout()) {
      $rows[$key]['flex'] = [
        '#markup' => empty($element['#flex']) ? 1 : $element['#flex'],
      ];
    }
    if ($yamlform_element
      ->hasProperty('required')) {
      $rows[$key]['required'] = [
        '#type' => 'checkbox',
        '#default_value' => empty($element['#required']) ? FALSE : TRUE,
      ];
    }
    else {
      $rows[$key]['required'] = [
        '#markup' => '',
      ];
    }
    $rows[$key]['weight'] = [
      '#type' => 'weight',
      '#title' => $this
        ->t('Weight for ID @id', [
        '@id' => $key,
      ]),
      '#title_display' => 'invisible',
      '#default_value' => $element['#weight'],
      '#attributes' => [
        'class' => [
          'row-weight',
        ],
      ],
      '#delta' => $delta,
    ];
    $rows[$key]['parent']['key'] = [
      '#parents' => [
        'elements_reordered',
        $key,
        'key',
      ],
      '#type' => 'hidden',
      '#value' => $key,
      '#attributes' => [
        'class' => [
          'row-key',
        ],
      ],
    ];
    $rows[$key]['parent']['parent_key'] = [
      '#parents' => [
        'elements_reordered',
        $key,
        'parent_key',
      ],
      '#type' => 'textfield',
      '#size' => 20,
      '#title' => $this
        ->t('Parent'),
      '#title_display' => 'invisible',
      '#default_value' => $element['#yamlform_parent_key'],
      '#attributes' => [
        'class' => [
          'row-parent-key',
        ],
        'readonly' => 'readonly',
      ],
    ];
    if (!$yamlform
      ->isNew()) {
      $rows[$key]['operations'] = [
        '#type' => 'operations',
      ];
      $rows[$key]['operations']['#links']['edit'] = [
        'title' => $this
          ->t('Edit'),
        'url' => new Url('entity.yamlform_ui.element.edit_form', [
          'yamlform' => $yamlform
            ->id(),
          'key' => $key,
        ]),
        'attributes' => $dialog_attributes,
      ];

      // Issue #2741877 Nested modals don't work: when using CKEditor in a
      // modal, then clicking the image button opens another modal,
      // which closes the original modal.
      // @todo Remove the below workaround once this issue is resolved.
      if ($yamlform_element
        ->getPluginId() == 'processed_text') {
        unset($rows[$key]['operations']['#links']['edit']['attributes']);
      }
      $rows[$key]['operations']['#links']['duplicate'] = [
        'title' => $this
          ->t('Duplicate'),
        'url' => new Url('entity.yamlform_ui.element.duplicate_form', [
          'yamlform' => $yamlform
            ->id(),
          'key' => $key,
        ]),
        'attributes' => $dialog_attributes,
      ];
      $rows[$key]['operations']['#links']['delete'] = [
        'title' => $this
          ->t('Delete'),
        'url' => new Url('entity.yamlform_ui.element.delete_form', [
          'yamlform' => $yamlform
            ->id(),
          'key' => $key,
        ]),
      ];
    }
  }

  // Must manually add local actions to the form because we can't alter local
  // actions and add the needed dialog attributes.
  // @see https://www.drupal.org/node/2585169
  $local_action_attributes = YamlFormDialogHelper::getModalDialogAttributes(800, [
    'button',
    'button-action',
    'button--primary',
    'button--small',
  ]);
  $form['local_actions'] = [
    '#prefix' => '<div class="yamlform-ui-local-actions">',
    '#suffix' => '</div>',
  ];
  $form['local_actions']['add_element'] = [
    '#type' => 'link',
    '#title' => $this
      ->t('Add element'),
    '#url' => new Url('entity.yamlform_ui.element', [
      'yamlform' => $yamlform
        ->id(),
    ]),
    '#attributes' => $local_action_attributes,
  ];
  if ($this->elementManager
    ->createInstance('yamlform_wizard_page')
    ->isEnabled()) {
    $form['local_actions']['add_page'] = [
      '#type' => 'link',
      '#title' => $this
        ->t('Add page'),
      '#url' => new Url('entity.yamlform_ui.element.add_form', [
        'yamlform' => $yamlform
          ->id(),
        'type' => 'wizard_page',
      ]),
      '#attributes' => $local_action_attributes,
    ];
  }
  if ($yamlform
    ->hasFlexboxLayout()) {
    $form['local_actions']['add_layout'] = [
      '#type' => 'link',
      '#title' => $this
        ->t('Add layout'),
      '#url' => new Url('entity.yamlform_ui.element.add_form', [
        'yamlform' => $yamlform
          ->id(),
        'type' => 'flexbox',
      ]),
      '#attributes' => $local_action_attributes,
    ];
  }
  $form['elements_reordered'] = [
    '#type' => 'table',
    '#header' => $header,
    '#empty' => $this
      ->t('Please add elements to this form.'),
    '#attributes' => [
      'class' => [
        'yamlform-ui-elements-table',
      ],
    ],
    '#tabledrag' => [
      [
        'action' => 'match',
        'relationship' => 'parent',
        'group' => 'row-parent-key',
        'source' => 'row-key',
        'hidden' => TRUE,
        /* hides the WEIGHT & PARENT tree columns below */
        'limit' => FALSE,
      ],
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'row-weight',
      ],
    ],
  ] + $rows;

  // Must preload libraries required by (modal) dialogs.
  $form['#attached']['library'][] = 'yamlform/yamlform.admin.dialog';
  $form['#attached']['library'][] = 'yamlform_ui/yamlform_ui';
  return $form;
}