isExposed example

protected function valueForm(&$form, FormStateInterface $form_state) {
    $users = $this->value ? User::loadMultiple($this->value) : [];
    $default_value = EntityAutocomplete::getEntityLabels($users);
    $form['value'] = [
      '#type' => 'entity_autocomplete',
      '#title' => $this->t('Usernames'),
      '#description' => $this->t('Enter a comma separated list of user names.'),
      '#target_type' => 'user',
      '#tags' => TRUE,
      '#default_value' => $default_value,
      '#process_default_value' => $this->isExposed(),
    ];

    $user_input = $form_state->getUserInput();
    if ($form_state->get('exposed') && !isset($user_input[$this->options['expose']['identifier']])) {
      $user_input[$this->options['expose']['identifier']] = $default_value;
      $form_state->setUserInput($user_input);
    }
  }

  protected function valueValidate($form, FormStateInterface $form_state) {
    $uids = [];
    

  public function canExpose() {
    return TRUE;
  }

  /** * Determine if a filter can be converted into a group. * * Only exposed filters with operators available can be converted into groups. */
  protected function canBuildGroup() {
    return $this->isExposed() && (count($this->operatorOptions()) > 0);
  }

  /** * Returns TRUE if the exposed filter works like a grouped filter. */
  public function isAGroup() {
    return $this->isExposed() && !empty($this->options['is_grouped']);
  }

  /** * Provide the basic form which calls through to subforms. * * If overridden, it is best to call through to the parent, * or to at least make sure all of the functions in this form * are called. */


    // Validate that the sort field identifier is unique within the sort     // handlers. Note that the sort field identifier is different that other     // identifiers because it is used as a query string value of the 'sort_by'     // parameter, while the others are used as query string parameter keys.     // Therefore we can have a sort field identifier be the same as an exposed     // filter identifier. This prevents us from using     // DisplayPluginInterface::isIdentifierUnique() to test for uniqueness.     // @see \Drupal\views\Plugin\views\display\DisplayPluginInterface::isIdentifierUnique()     foreach ($this->view->display_handler->getHandlers('sort') as $key => $handler) {
      if ($handler->canExpose() && $handler->isExposed()) {
        if ($form_state->get('id') !== $key && isset($handler->options['expose']['field_identifier']) && $field_identifier === $handler->options['expose']['field_identifier']) {
          $form_state->setErrorByName('expose][field_identifier', $this->t('This identifier is already used by %label sort handler.', [
            '%label' => $handler->adminLabel(TRUE),
          ]));
          return;
        }
      }
    }
  }

  /** * {@inheritdoc} */
$form_state->setValue(['exposed_form_options', 'text_input_required_format']$exposed_form_options['text_input_required']['format']);
    $form_state->setValue(['exposed_form_options', 'text_input_required']$exposed_form_options['text_input_required']['value']);
    parent::submitOptionsForm($form$form_state);
  }

  protected function exposedFilterApplied() {
    static $cache = NULL;
    if (!isset($cache)) {
      $view = $this->view;
      if (is_array($view->filter) && count($view->filter)) {
        foreach ($view->filter as $filter) {
          if ($filter->isExposed()) {
            $identifier = $filter->options['expose']['identifier'];
            if (isset($view->getExposedInput()[$identifier])) {
              $cache = TRUE;
              return $cache;
            }
          }
        }
      }
      $cache = FALSE;
    }

    
// Check if the form was already created     if ($cache = $this->exposedFormCache->getForm($view->storage->id()$view->current_display)) {
      return $cache;
    }

    $form['#info'] = [];

    // Go through each handler and let it generate its exposed widget.     foreach ($view->display_handler->handlers as $type => $value) {
      /** @var \Drupal\views\Plugin\views\ViewsHandlerInterface $handler */
      foreach ($view->$type as $id => $handler) {
        if ($handler->canExpose() && $handler->isExposed()) {
          // Grouped exposed filters have their own forms.           // Instead of render the standard exposed form, a new Select or           // Radio form field is rendered with the available groups.           // When a user chooses an option the selected value is split           // into the operator and value that the item represents.           if ($handler->isAGroup()) {
            $handler->groupForm($form$form_state);
            $id = $handler->options['group_info']['identifier'];
          }
          else {
            $handler->buildExposedForm($form$form_state);
          }
public function isDefaultDisplay() {
    return FALSE;
  }

  /** * {@inheritdoc} */
  public function usesExposed() {
    if (!isset($this->has_exposed)) {
      foreach ($this->handlers as $type => $value) {
        foreach ($this->view->$type as $handler) {
          if ($handler->canExpose() && $handler->isExposed()) {
            // One is all we need; if we find it, return TRUE.             $this->has_exposed = TRUE;
            return TRUE;
          }
        }
      }
      $pager = $this->getPlugin('pager');
      if (isset($pager) && $pager->usesExposed()) {
        $this->has_exposed = TRUE;
        return TRUE;
      }
      

  public function query() {
    $view = $this->view;
    $exposed_data = $view->exposed_data ?? [];
    $sort_by = $exposed_data['sort_by'] ?? NULL;
    if (!empty($sort_by)) {
      // Make sure the original order of sorts is preserved       // (e.g. a sticky sort is often first)       $view->query->orderby = [];
      foreach ($view->sort as $sort) {
        if (!$sort->isExposed()) {
          $sort->query();
        }
        elseif (!empty($sort->options['expose']['field_identifier']) && $sort->options['expose']['field_identifier'] === $sort_by) {
          if (isset($exposed_data['sort_order']) && in_array($exposed_data['sort_order']['ASC', 'DESC'], TRUE)) {
            $sort->options['order'] = $exposed_data['sort_order'];
          }
          $sort->setRelationship();
          $sort->query();
        }
      }
    }
  }
Home | Imprint | This part of the site doesn't use cookies.