canExpose example

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;
      }
      
// 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);
          }


  /** * 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. */
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
    parent::buildOptionsForm($form$form_state);
    if ($this->canExpose()) {
      $this->showExposeButton($form$form_state);
    }
    if ($this->canBuildGroup()) {
      $this->showBuildGroupButton($form$form_state);
    }
    $form['clear_markup_start'] = [
      '#markup' => '<div class="clearfix">',
    ];
    if ($this->isAGroup()) {
      if ($this->canBuildGroup()) {
        $form['clear_markup_start'] = [
          

  public function exposedFormAlter(&$form, FormStateInterface $form_state) {
    if (!empty($this->options['submit_button'])) {
      $form['actions']['submit']['#value'] = $this->options['submit_button'];
    }

    // Check if there is exposed sorts for this view     $exposed_sorts = [];
    $exposed_sorts_options = [];
    foreach ($this->view->sort as $id => $handler) {
      if ($handler->canExpose() && $handler->isExposed() && !empty($handler->options['expose']['field_identifier'])) {
        $exposed_sorts[$handler->options['expose']['field_identifier']] = $id;
        $exposed_sorts_options[$handler->options['expose']['field_identifier']] = $handler->options['expose']['label'];
      }
    }

    if (count($exposed_sorts)) {
      $form['sort_by'] = [
        '#type' => 'select',
        '#options' => $exposed_sorts_options,
        '#title' => $this->options['exposed_sorts_label'],
      ];
      
case 'DESC';
      case 'desc';
        return $this->t('desc');
    }
  }

  /** * Basic options for all sort criteria. */
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
    parent::buildOptionsForm($form$form_state);
    if ($this->canExpose()) {
      $this->showExposeButton($form$form_state);
    }
    $form['op_val_start'] = ['#value' => '<div class="clearfix">'];
    $this->showSortForm($form$form_state);
    $form['op_val_end'] = ['#value' => '</div>'];
    if ($this->canExpose()) {
      $this->showExposeForm($form$form_state);
    }
  }

  /** * Shortcut to display the expose/hide button. */
Home | Imprint | This part of the site doesn't use cookies.