You are here

public function AddForm::buildForm in Favorites 8.2

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

File

src/Form/AddForm.php, line 41
Contains Drupal\favorites\Form\AddForm

Class

AddForm
Class AddForm.

Namespace

Drupal\favorites\Form

Code

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

  // @todo necessary? D8 requires >=5.5.9 anyway?
  if (function_exists('version_compare') && version_compare(PHP_VERSION, '5.1.0', '>=')) {
    $request = \Drupal::request();
    $route_match = \Drupal::routeMatch();
    $title = \Drupal::service('title_resolver')
      ->getTitle($request, $route_match
      ->getRouteObject());
  }
  if (!isset($title)) {
    $title = \Drupal::config('core.site_information')
      ->get('site_name');
  }
  if ($title == '') {
    $title = $this
      ->t('Home', array(), array(
      'context' => 'Home page',
    ));
  }
  $title = strip_tags($title);
  $path = \Drupal::service('path.current')
    ->getPath();
  $query = isset($_GET['keys']) ? UrlHelper::buildQuery($_GET) : '';
  $form = array(
    'add' => array(
      '#type' => 'details',
      '#title' => $this
        ->t('Add this page'),
      'title' => array(
        '#type' => 'textfield',
        '#size' => 20,
        '#maxlength' => 255,
        '#default_value' => $title,
        '#attributes' => array(
          'style' => 'width: 90%',
          'class' => array(
            'favorites-add-textfield',
          ),
        ),
      ),
      'path' => array(
        '#type' => 'hidden',
        '#value' => $path,
      ),
      'query' => array(
        '#type' => 'hidden',
        '#value' => $query,
      ),
      'submit' => array(
        '#type' => 'submit',
        '#value' => $this
          ->t('Add', array(), array(
          'context' => 'Add a favorite to the list',
        )),
        '#ajax' => array(
          'url' => Url::fromRoute('favorites.add'),
        ),
      ),
    ),
  );
  return $form;
}