You are here

public function TourForm::form in Tour UI 8

Gets the actual form array to be built.

Overrides EntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

src/Form/TourForm.php, line 83

Class

TourForm
Form controller for the tour entity edit forms.

Namespace

Drupal\tour_ui\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $tour = $this->entity;
  $form = parent::form($form, $form_state);
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Tour name'),
    '#required' => TRUE,
    '#default_value' => $tour
      ->label(),
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#machine_name' => [
      'exists' => '\\Drupal\\tour\\Entity\\Tour::load',
      'replace_pattern' => '[^a-z0-9-]+',
      'replace' => '-',
    ],
    '#default_value' => $tour
      ->id(),
    '#disabled' => !$tour
      ->isNew(),
  ];
  $form['langcode'] = [
    '#type' => 'language_select',
    '#title' => $this
      ->t('Language'),
    '#languages' => LanguageInterface::STATE_ALL,
    // Default to the content language opposed to und (no language).
    '#default_value' => empty($tour
      ->language()) ? $this->languageManager
      ->getCurrentLanguage()
      ->getId() : $tour
      ->language()
      ->getId(),
  ];
  $form['module'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Module name'),
    '#description' => $this
      ->t('Each tour needs a module.'),
    '#required' => TRUE,
    '#autocomplete_route_name' => 'tour_ui.get_modules',
    '#default_value' => $tour
      ->get('module'),
  ];
  $default_routes = [];
  if ($routes = $tour
    ->getRoutes()) {
    foreach ($routes as $route) {
      $default_routes[] = $route['route_name'];
      if (isset($route['route_params'])) {
        foreach ($route['route_params'] as $key => $value) {
          $default_routes[] = '- ' . $key . ':' . $value;
        }
      }
    }
  }
  $form['routes'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Routes'),
    '#default_value' => implode("\n", $default_routes),
    '#rows' => 5,
    '#description' => $this
      ->t('Provide a list of routes that this tour will be displayed on. Add route_name first then optionally route parameters. For example <pre>entity.node.canonical<br/>- node:2</pre> will only show on the <em>node/2</em> page.<br/>NOTE: route parameters are <strong>not validated yet</strong>.'),
  ];
  $form['find-routes'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Find route or path fragment'),
    '#description' => $this
      ->t('You can type a route name or path fragment.'),
    '#required' => FALSE,
    '#autocomplete_route_name' => 'tour_ui.get_routes',
  ];

  // Don't show the tips on the inital add.
  if ($tour
    ->isNew()) {
    return $form;
  }

  // Start building the list of tips assigned to this tour.
  $form['tips'] = [
    '#type' => 'table',
    '#header' => [
      $this
        ->t('Label'),
      $this
        ->t('Weight'),
      $this
        ->t('Operations'),
    ],
    '#caption' => [
      [
        '#markup' => $this
          ->t('Tips provided by this tour. By clicking on Operations buttons, every changes which are not saved will be lost.'),
      ],
    ],
    '#tabledrag' => [
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'tip-order-weight',
      ],
    ],
    '#weight' => 40,
  ];

  // Populate the table with the assigned tips.
  $tips = $tour
    ->getTips();
  if (!empty($tips)) {
    foreach ($tips as $tip) {
      $tip_id = $tip
        ->get('id');
      try {
        $form['#data'][$tip_id] = $tip
          ->getConfiguration();
      } catch (\Error $e) {
        $this->messenger
          ->addMessage($this
          ->t('Tip %tip is not configurable. You cannot save this tour.', [
          '%tip' => $tip
            ->getLabel(),
        ]), 'warning');
      }
      $form['tips'][$tip_id]['#attributes']['class'][] = 'draggable';
      $form['tips'][$tip_id]['label'] = [
        '#plain_text' => $tip
          ->get('label'),
      ];
      $form['tips'][$tip_id]['weight'] = [
        '#type' => 'weight',
        '#title' => $this
          ->t('Weight for @title', [
          '@title' => $tip
            ->get('label'),
        ]),
        '#delta' => 100,
        '#title_display' => 'invisible',
        '#default_value' => $tip
          ->get('weight'),
        '#attributes' => [
          'class' => [
            'tip-order-weight',
          ],
        ],
      ];

      // Provide operations links for the tip.
      $links = [];
      if (method_exists($tip, 'buildConfigurationForm')) {
        $links['edit'] = [
          'title' => $this
            ->t('Edit'),
          'url' => Url::fromRoute('tour_ui.tip.edit', [
            'tour' => $tour
              ->id(),
            'tip' => $tip_id,
          ]),
        ];
      }
      $links['delete'] = [
        'title' => $this
          ->t('Delete'),
        'url' => Url::fromRoute('tour_ui.tip.delete', [
          'tour' => $tour
            ->id(),
          'tip' => $tip_id,
        ]),
      ];
      $form['tips'][$tip_id]['operations'] = [
        '#type' => 'operations',
        '#links' => $links,
      ];
    }
  }

  // Build the new tour tip addition form and add it to the tips list.
  $tip_definitions = $this->tipPluginManager
    ->getDefinitions();
  $tip_definition_options = [];
  foreach ($tip_definitions as $tip => $definition) {
    if (method_exists($definition['class'], 'buildConfigurationForm')) {
      $tip_definition_options[$tip] = $definition['title'];
    }
  }
  $user_input = $form_state
    ->getUserInput();
  $form['tips']['new'] = [
    '#tree' => FALSE,
    '#weight' => isset($user_input['weight']) ? $user_input['weight'] : 0,
    '#attributes' => [
      'class' => [
        'draggable',
      ],
    ],
  ];
  $form['tips']['new']['new'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Tip'),
    '#title_display' => 'invisible',
    '#options' => $tip_definition_options,
    '#empty_option' => $this
      ->t('Select a new tip'),
  ];
  $form['tips']['new']['weight'] = [
    '#type' => 'weight',
    '#title' => $this
      ->t('Weight for new tip'),
    '#title_display' => 'invisible',
    '#default_value' => count($form['tips']) - 1,
    '#attributes' => [
      'class' => [
        'tip-order-weight',
      ],
    ],
  ];
  $form['tips']['new']['add'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Add'),
    '#validate' => [
      [
        $this,
        'tipValidate',
      ],
    ],
    '#submit' => [
      [
        $this,
        'tipAdd',
      ],
    ],
  ];
  return $form;
}