You are here

public function SavedSearchForm::buildEntity in Search API Saved Searches 8

Builds an updated entity object based upon the submitted form values.

For building the updated entity object the form's entity is cloned and the submitted form values are copied to entity properties. The form's entity remains unchanged.

Parameters

array $form: A nested array form elements comprising the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

\Drupal\Core\Entity\EntityInterface An updated copy of the form's entity object.

Overrides ContentEntityForm::buildEntity

See also

\Drupal\Core\Entity\EntityFormInterface::getEntity()

File

src/Form/SavedSearchForm.php, line 42

Class

SavedSearchForm
Provides a standard form for editing saved searches.

Namespace

Drupal\search_api_saved_searches\Form

Code

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

  /** @var \Drupal\search_api_saved_searches\SavedSearchInterface $search */
  $search = parent::buildEntity($form, $form_state);
  if ($form_state
    ->hasValue('search_keywords')) {
    $keywords = trim($form_state
      ->getValue('search_keywords'));
    if ($keywords === '') {
      $keywords = NULL;
    }
    $query = $search
      ->getQuery();
    $query
      ->keys($keywords);
    $search
      ->setQuery($query);
  }
  return $search;
}