You are here

public function SearchForm::buildForm in Entity Reference Tree Widget 8

Same name and namespace in other branches
  1. 2.x src/Form/SearchForm.php \Drupal\entity_reference_tree\Form\SearchForm::buildForm()

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/SearchForm.php, line 40

Class

SearchForm
ModalForm class.

Namespace

Drupal\entity_reference_tree\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $field_edit_id = '', $bundles = '', $entity_type = '', $theme = 'default', $dots = false) {

  // Do nothing after the form is submitted.
  if (!empty($form_state
    ->getValues())) {
    return [];
  }

  // Limit number of selected nodes of tree.
  $limit = $this
    ->getRequest()
    ->get('limit');

  // The status messages that will contain any form errors.
  $form['status_messages'] = [
    '#type' => 'status_messages',
    '#weight' => -10,
  ];

  // Selected entity text.
  $form['selected_text'] = [
    '#type' => 'html_tag',
    '#tag' => 'div',
    '#value' => $this
      ->t('Selected Entities'),
    '#weight' => 1000,
    '#attributes' => [
      'class' => [
        'selected-entities-text',
      ],
      'id' => [
        'entity-reference-tree-selected-text',
      ],
    ],
  ];

  // Hidden field for submitting selected entity IDs.
  $form['selected_node'] = [
    '#type' => 'hidden',
    '#attributes' => [
      'id' => [
        'entity-reference-tree-selected-node',
      ],
    ],
  ];

  // Search filter box.
  $form['tree_search'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Search'),
    '#size' => 60,
    '#attributes' => [
      'id' => [
        'entity-reference-tree-search',
      ],
    ],
  ];

  // JsTree container.
  $form['tree_container'] = [
    '#type' => 'html_tag',
    '#tag' => 'div',
    '#attributes' => [
      'id' => [
        'entity-reference-tree-wrapper',
      ],
      'theme' => $theme,
      'dots' => $dots,
    ],
  ];

  // Submit button.
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['send'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Save'),
    '#attributes' => [
      'class' => [
        'use-ajax',
      ],
    ],
    '#ajax' => [
      'callback' => [
        $this,
        'submitForm',
      ],
      'event' => 'click',
    ],
  ];
  $form['#attached']['library'][] = 'entity_reference_tree/jstree';
  $form['#attached']['library'][] = 'entity_reference_tree/entity_tree';
  $form['#attached']['library'][] = 'entity_reference_tree/jstree_' . $theme . '_theme';

  // Disable the cache for this form.
  $form_state
    ->setCached(FALSE);
  $form['#cache'] = [
    'max-age' => 0,
  ];
  $form['#attributes']['data-user-info-from-browser'] = FALSE;

  // Field element id.
  $form['field_id'] = [
    '#name' => 'field_id',
    '#type' => 'hidden',
    '#weight' => 80,
    '#value' => $field_edit_id,
    '#attributes' => [
      'id' => [
        'entity-reference-tree-widget-field',
      ],
    ],
  ];

  // Entity type.
  $form['entity_type'] = [
    '#name' => 'entity_type',
    '#type' => 'hidden',
    '#weight' => 80,
    '#value' => $entity_type,
    '#attributes' => [
      'id' => [
        'entity-reference-tree-entity-type',
      ],
    ],
  ];

  // Entity bundle.
  $form['entity_bundle'] = [
    '#name' => 'entity_bundle',
    '#type' => 'hidden',
    '#weight' => 80,
    '#value' => $bundles,
    '#attributes' => [
      'id' => [
        'entity-reference-tree-entity-bundle',
      ],
    ],
  ];

  // Pass data to js file.
  $form['#attached']['drupalSettings'] = [
    'entity_tree_token_' . $field_edit_id => \Drupal::csrfToken()
      ->get($bundles),
    'tree_limit_' . $field_edit_id => empty($limit) ? -1 : $limit,
  ];
  return $form;
}