View source
<?php
namespace Drupal\entity_reference_tree\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\CloseModalDialogCommand;
use Drupal\Core\Ajax\ReplaceCommand;
use Drupal\Core\Ajax\InvokeCommand;
class SearchForm extends FormBase {
public function getFormId() {
return 'entity_reference_tree_search_form';
}
public function buildForm(array $form, FormStateInterface $form_state, $field_edit_id = '', $bundles = '', $entity_type = '', $theme = 'default', $dots = false) {
if (!empty($form_state
->getValues())) {
return [];
}
$limit = $this
->getRequest()
->get('limit');
$form['status_messages'] = [
'#type' => 'status_messages',
'#weight' => -10,
];
$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',
],
],
];
$form['selected_node'] = [
'#type' => 'hidden',
'#attributes' => [
'id' => [
'entity-reference-tree-selected-node',
],
],
];
$form['tree_search'] = [
'#type' => 'textfield',
'#title' => $this
->t('Search'),
'#size' => 60,
'#attributes' => [
'id' => [
'entity-reference-tree-search',
],
],
];
$form['tree_container'] = [
'#type' => 'html_tag',
'#tag' => 'div',
'#attributes' => [
'id' => [
'entity-reference-tree-wrapper',
],
'theme' => $theme,
'dots' => $dots,
],
];
$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';
$form_state
->setCached(FALSE);
$form['#cache'] = [
'max-age' => 0,
];
$form['#attributes']['data-user-info-from-browser'] = FALSE;
$form['field_id'] = [
'#name' => 'field_id',
'#type' => 'hidden',
'#weight' => 80,
'#value' => $field_edit_id,
'#attributes' => [
'id' => [
'entity-reference-tree-widget-field',
],
],
];
$form['entity_type'] = [
'#name' => 'entity_type',
'#type' => 'hidden',
'#weight' => 80,
'#value' => $entity_type,
'#attributes' => [
'id' => [
'entity-reference-tree-entity-type',
],
],
];
$form['entity_bundle'] = [
'#name' => 'entity_bundle',
'#type' => 'hidden',
'#weight' => 80,
'#value' => $bundles,
'#attributes' => [
'id' => [
'entity-reference-tree-entity-bundle',
],
],
];
$form['#attached']['drupalSettings'] = [
'entity_tree_token_' . $field_edit_id => \Drupal::csrfToken()
->get($bundles),
'tree_limit_' . $field_edit_id => empty($limit) ? -1 : $limit,
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$response = new AjaxResponse();
if ($form_state
->hasAnyErrors()) {
$response
->addCommand(new ReplaceCommand('#entity_reference_tree_wrapper', $form));
}
else {
$response
->addCommand(new InvokeCommand(NULL, 'entitySearchDialogAjaxCallback', [
$form_state
->getValue('field_id'),
$form_state
->getValue('selected_node'),
]));
$response
->addCommand(new CloseModalDialogCommand());
}
return $response;
}
}