You are here

public function FormAssemblyEntityForm::buildForm in FormAssembly 8

Form constructor.

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 EntityForm::buildForm

File

src/Form/FormAssemblyEntityForm.php, line 50

Class

FormAssemblyEntityForm
Form controller for FormAssembly Form edit forms.

Namespace

Drupal\formassembly\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $entity = $this->entity;
  if (!$entity instanceof FormAssemblyEntity) {
    return $form;
  }
  $form = parent::buildForm($form, $form_state);
  $weight = isset($form['query_params']['#weight']) ? $form['query_params']['#weight'] + 1 : 90;
  $token_display = $this
    ->buildTokenElement($weight, 'The entity type of any path parameter which resolves to an entity will be added to the token types at render.');
  if (!empty($token_display)) {
    $form['token_link'] = $token_display;
  }
  if (class_exists('Highlight\\Highlighter') && $this->markup instanceof ApiMarkup) {
    $markup = $this->markup
      ->getFormMarkup($entity);
    $highlighter = new Highlighter();
    try {
      if (class_exists('Gajus\\Dindent\\Indenter')) {
        $indenter = new Indenter([
          'indentation_character' => '  ',
        ]);
        $markup = $indenter
          ->indent($markup);
      }
      $highlighted = $highlighter
        ->highlight('html', $markup);
      $form['inspector'] = [
        '#type' => 'details',
        '#title' => $this
          ->t('Inspect form html'),
        '#weight' => $weight + 1,
        'form_html' => [
          '#type' => 'html_tag',
          '#tag' => 'pre',
          '#attributes' => [
            'class' => [
              'hljs',
              $highlighted->language,
            ],
          ],
          '#attached' => [
            'library' => [
              'formassembly/highlighter',
            ],
          ],
          '#value' => $highlighted->value,
        ],
      ];
    } catch (\DomainException $e) {
      $this
        ->getLogger('FormAssembly')
        ->error('The code highlight service failed to render the form html.');
    } catch (InvalidArgumentException $e) {
      $this
        ->getLogger('FormAssembly')
        ->error('The code highlight service failed to render the form html.');
    } catch (RuntimeException $e) {
      $this
        ->getLogger('FormAssembly')
        ->error('The code highlight service failed to render the form html.');
    }
  }
  return $form;
}