diff --git a/src/Form/IndexAddFieldsForm.php b/src/Form/IndexAddFieldsForm.php index b534aac..9d76f8b 100644 --- a/src/Form/IndexAddFieldsForm.php +++ b/src/Form/IndexAddFieldsForm.php @@ -7,7 +7,6 @@ use Drupal\Component\Utility\Html; use Drupal\Core\Datetime\DateFormatterInterface; use Drupal\Component\Utility\NestedArray; -use Drupal\Core\Datetime\DateFormatter; use Drupal\Core\Entity\EntityForm; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\TypedData\EntityDataDefinitionInterface; @@ -16,7 +15,6 @@ use Drupal\Core\TypedData\ComplexDataDefinitionInterface; use Drupal\Core\TypedData\DataReferenceDefinitionInterface; use Drupal\Core\Url; -use Drupal\search_api\Datasource\DatasourceInterface; use Drupal\search_api\Processor\ConfigurablePropertyInterface; use Drupal\search_api\Processor\ProcessorPropertyInterface; use Drupal\search_api\Utility\FieldsHelperInterface; @@ -62,15 +60,6 @@ class IndexAddFieldsForm extends EntityForm { protected $unmappedFields = array(); /** - * REMOVE. - * - * Counter keeping track of the sequence of method invocation. - * - * @var int - */ - protected static $sequenceCounter = 0; - - /** * {@inheritdoc} */ public function getFormId() { @@ -104,8 +93,6 @@ public function __construct(EntityTypeManagerInterface $entity_type_manager, Fie $this->renderer = $renderer; $this->dateFormatter = $date_formatter; $this->parameters = $parameters; - // REMOVE. - $this->displayMethodInvocation('__construct'); } /** @@ -122,7 +109,6 @@ public static function create(ContainerInterface $container) { return new static($entity_type_manager, $fields_helper, $renderer, $date_formatter, $parameters); } - /** * Retrieves a single page request parameter. * @@ -142,9 +128,6 @@ public function getParameter($name, $default = NULL) { * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { - - $this->displayMethodInvocation('buildForm'); - $index = $this->entity; // Do not allow the form to be cached. See @@ -153,7 +136,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { $this->checkEntityEditable($form, $index); - // Set title. Strip html to accomodate modal title. + // Set title. Strip html to accommodate modal title. $args['%index'] = $index->label(); $form['#title'] = PlainTextOutput::renderFromHtml($this->t('Add fields to index %index', $args)); @@ -163,27 +146,16 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#attributes' => ['id' => 'index-fields'], ]; - // TEST. - $form['index-fields']['messages'] = [ - '#type' => 'container', - '#attributes' => ['id' => 'message-wrapper'], - ]; - - // We store the datasources we need to show in the form - // to accomodate the ajax magic. + // We store the datasources we need to show in the form to accommodate the + // AJAX magic. // Create hidden form field to store datasources. $form['datasources'] = array( '#type' => 'hidden', ); + $datasources = $form_state->getValue('datasources'); // First set some defaults when nothing is set. - $form_state_datasources = $form_state->getValue('datasources'); - - dsm('form state at start of build form'); - dsm($form_state_datasources); - - if (empty($form_state_datasources)) { - dsm('leeg dus invullen'); + if (empty($datasources)) { // Get the datasources to add. // Start with the general fields. $datasources = ['general' => NULL]; @@ -194,16 +166,10 @@ public function buildForm(array $form, FormStateInterface $form_state) { $datasource_id = $entity_datasource->getPluginId(); $datasources[$datasource_id] = NULL; } - $form['datasources']['#value'] = $datasources; - } - else { - dsm('niet leeg'); - $form['datasources']['#value'] = $form_state_datasources; } - dsm('deze datasources moeten we weergeven'); - dsm($form['datasources']['#value']); + $form['datasources']['#value'] = $datasources; - // Create form elementes for each of the datasources we want to show. + // Create form elements for each of the datasources we want to show. // This is a recursive function, allowing to show the nested elements. $containers = $this->createFieldContainers($form['datasources']['#value']); $form['index-fields']['properties'] = $containers; @@ -237,96 +203,61 @@ public function buildForm(array $form, FormStateInterface $form_state) { } /** - * Create form elements for each field. + * Creates form elements for each field. * * @param array $fields * A nested array which defines what fields to show. This info is * stored in the form. * * @return array - * Return form elements. + * Form elements for the given fields.. */ protected function createFieldContainers(array $fields) { - foreach (array_keys($fields) as $datasource_id) { + $items = array(); + $url = $this->entity->toUrl('add-fields'); - // Each element get's a container so we can add children for subelements. - $item = array( + foreach (array_keys($fields) as $datasource_id) { + // Each element get' a container so we can add children for sub elements. + $item = [ '#type' => 'container', - '#attributes' => array( - 'class' => array('container-inline'), + '#attributes' => [ + 'class' => ['container-inline'], '#attributes' => ['id' => '' . $datasource_id], - ), + ], '#field-name' => $datasource_id, - ); + ]; - $item['test'] = array( + $item['test'] = [ '#markup' => 'Show ' . $datasource_id . ' in ' . 'index-field-' . $datasource_id . ' container', - ); + ]; $active = FALSE; - $active_datasource = $this->getParameter('datasource'); if (isset($active_datasource)) { $active = $active_datasource == $datasource_id; } - $url = $this->entity->toUrl('add-fields'); - if ($active) { - $expand_link = array( - '#type' => 'link', - '#title' => '(-) ', - '#url' => $url, - ); - } - else { - $url->setOption('query', array('datasource' => $datasource_id)); - $expand_link = array( - '#type' => 'link', - '#title' => '(+) ', - '#url' => $url, - ); - $expand_button = array( - '#type' => 'submit', - '#name' => 'expand_datasource_' . $datasource_id, - '#value' => '(+)', - '#submit' => ['::expandDatasourceFormSubmit'], - '#ajax' => [ - 'callback' => '::expandDatasourceFormAjax', - 'wrapper' => 'index-fields', - 'effect' => 'fade', - 'method' => 'replace', - ], - ); - - } - $item['expand_link'] = $expand_link; - $item['expand_button'] = $expand_button; - - //$label = $fields[$datasource_id] ? $fields[$datasource_id] : $this->t('General'); - //$item['label']['#markup'] = $label; - - if ($active) { - $properties = $this->entity->getPropertyDefinitions($datasource_id == 'general' ? NULL: $datasource_id); - if ($properties) { + $properties = $this->entity->getPropertyDefinitions($datasource_id == 'general' ? NULL : $datasource_id); + if ($properties) { + $active_property_path = NULL; + if ($active) { $active_property_path = $this->getParameter('property_path', ''); - $base_url = clone $url; - $base_url->setOption('query', array('datasource' => $datasource_id)); - $item['properties'] = $this->getPropertiesList($properties, $active_property_path, $base_url); } + $base_url = clone $url; + $base_url->setOption('query', array('datasource' => $datasource_id)); + $item['properties'] = $this->getPropertiesList($properties, $active_property_path, $base_url); } $items[] = $item; // Add child elements. - if(is_array($fields[$datasource_id])){ - // Add new things. + if (is_array($fields[$datasource_id])) { $new_things = $this->createFieldContainers($fields[$datasource_id]); - foreach($new_things as $new_thing){ + foreach ($new_things as $new_thing) { $items[] = $new_thing; } } - - } + return $items; } @@ -334,9 +265,6 @@ protected function createFieldContainers(array $fields) { * Handles form submissions for expanding a datasource. */ public function expandDatasourceFormSubmit(&$form, FormStateInterface &$form_state) { - // REMOVE. - $this->displayMethodInvocation('expandDatasourceFormSubmit'); - // Find the triggering field. $button = $form_state->getTriggeringElement(); // Go one level up in the form, to the widgets container. @@ -348,86 +276,22 @@ public function expandDatasourceFormSubmit(&$form, FormStateInterface &$form_sta $properties = $this->entity->getPropertyDefinitions($field_name == 'general' ? NULL : $field_name); - //dsm(array_keys($properties)); - dsm('opgevraagde datasources'); - dsm($datasources); - foreach(array_keys($properties) as $property){ + foreach (array_keys($properties) as $property) { $datasources[$field_name][$property] = NULL; } - dsm('datasources changes uit ajax submit'); - dsm($datasources); $form_state->setValue('datasources', $datasources); - dsm('opgeslaan in datasources?'); - dsm($form_state->getValue('datasources')); $form_state->setRebuild(); } /** - * Return new datasource structure. + * Returns new datasource structure. */ public function expandDatasourceFormAjax(array $form, FormStateInterface &$form_state) { - - - $this->displayMethodInvocation('expandDatasourceFormAjax'); - //dsm($form_state->getValue('datasource')); - //$form = $form_state->getCompleteForm(); - //$button = $form_state->getTriggeringElement(); - - // Go one level up in the form, to the widgets container. - //dsm('array of button parrents'); - //dsm($button['#array_parents']); - //$element = NestedArray::getValue($form, array_slice($button['#array_parents'], 0, -3)); - //echo 'Which element trigggered?'; - //dsm('Which element trigggered?'); - - //dsm(array_keys($element)); - //dsm($element['#type']); - //dsm($element['field-name']); - $form['messages']['status'] = [ '#type' => 'status_messages', ]; -//dsm($form['datasources']['#value']); - - //$datasources = $form_state->getValue('datasources'); - //dsm($datasources); - //$form['datasources']['#value'] = $datasources; - //return $form['messages']; - //return $form['index-fields']; - //return $element; return $form; - - /* - * $button = $form_state->getTriggeringElement(); - - - // Ensure the widget allows adding additional items. - if ($element['#cardinality'] != FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) { - return; - } - - // Add a DIV around the delta receiving the Ajax effect. - $delta = $element['#max_delta']; - $element[$delta]['#prefix'] = '
' . (isset($element[$delta]['#prefix']) ? $element[$delta]['#prefix'] : ''); - $element[$delta]['#suffix'] = (isset($element[$delta]['#suffix']) ? $element[$delta]['#suffix'] : '') . '
'; - - return $element; - */ - } - - /** - * Update form processing information. - * - * Display the method being called and it's sequence in the form - * processing. - * - * @param string $method_name - * The method being invoked. - */ - private function displayMethodInvocation($method_name) { - self::$sequenceCounter++; - drupal_set_message(self::$sequenceCounter . ". $method_name"); } /** @@ -450,7 +314,6 @@ private function displayMethodInvocation($method_name) { * properties. */ protected function getPropertiesList(array $properties, $active_property_path, Url $base_url, $parent_path = '', $label_prefix = '') { - $list = array( '#theme' => 'search_api_form_item_list', ); @@ -523,8 +386,8 @@ protected function getPropertiesList(array $properties, $active_property_path, U continue; } - $nested_list = array(); - $expand_link = array(); + $nested_list = FALSE; + $expand_link = FALSE; if ($nested_properties) { if ($key == $active_item) { $link_url = clone $base_url; @@ -534,6 +397,11 @@ protected function getPropertiesList(array $properties, $active_property_path, U '#type' => 'link', '#title' => '(-) ', '#url' => $link_url, + '#attributes' => [ + 'class' => [ + 'use-ajax', + ], + ], ); $nested_list = $this->getPropertiesList($nested_properties, $active_property_path, $base_url, $this_path, $label_prefix . $label . ' ยป '); @@ -546,16 +414,10 @@ protected function getPropertiesList(array $properties, $active_property_path, U '#type' => 'link', '#title' => '(+) ', '#url' => $link_url, - ); - $expand_button = array( - '#type' => 'button', - '#name' => 'widget_configure', - '#value' => '(+)', - '#limit_validation_errors' => [['widget']], - '#submit' => ['::submitAjaxWidgetConfigForm'], - '#ajax' => [ - 'callback' => '::buildAjaxWidgetConfigForm', - 'wrapper' => 'index-field-list', + '#attributes' => [ + 'class' => [ + 'use-ajax', + ], ], ); } @@ -571,9 +433,6 @@ protected function getPropertiesList(array $properties, $active_property_path, U if ($expand_link) { $item['expand_link'] = $expand_link; } - if ($expand_button) { - $item['expand_button'] = $expand_button; - } $item['label']['#markup'] = Html::escape($label) . ' ';