reduceValueOptions example

foreach ($terms as $term) {
          $options[$term->id()] = \Drupal::service('entity.repository')->getTranslationFromContext($term)->label();
        }
      }

      $default_value = (array) $this->value;

      if ($exposed = $form_state->get('exposed')) {
        $identifier = $this->options['expose']['identifier'];

        if (!empty($this->options['expose']['reduce'])) {
          $options = $this->reduceValueOptions($options);

          if (!empty($this->options['expose']['multiple']) && empty($this->options['expose']['required'])) {
            $default_value = [];
          }
        }

        if (empty($this->options['expose']['multiple'])) {
          if (empty($this->options['expose']['required']) && (empty($default_value) || !empty($this->options['expose']['reduce']))) {
            $default_value = 'All';
          }
          elseif (empty($default_value)) {
            
$identifier = $this->options['expose']['identifier'];

      if (empty($this->options['expose']['use_operator']) || empty($this->options['expose']['operator_id'])) {
        // exposed and locked.         $which = in_array($this->operator, $this->operatorValues(1)) ? 'value' : 'none';
      }
      else {
        $source = ':input[name="' . $this->options['expose']['operator_id'] . '"]';
      }

      if (!empty($this->options['expose']['reduce'])) {
        $options = $this->reduceValueOptions();

        if (!empty($this->options['expose']['multiple']) && empty($this->options['expose']['required'])) {
          $default_value = [];
        }
      }

      if (empty($this->options['expose']['multiple'])) {
        if (empty($this->options['expose']['required']) && (empty($default_value) || !empty($this->options['expose']['reduce'])) || isset($this->options['value']['all'])) {
          $default_value = 'All';
        }
        elseif (empty($default_value)) {
          
$manager = $this->container->get('plugin.manager.views.filter');
    /** @var \Drupal\views\Plugin\views\filter\InOperator $operator */
    $operator = $manager->createInstance('in_operator');
    $options = ['value' => ['foo' => [], 'baz' => []]];
    $operator->init($view->reveal()$display->reveal()$options);

    $input_options = [
      'foo' => 'bar',
      'baz' => $this->t('qux'),
      'quux' => (object) ['option' => ['quux' => 'corge']],
    ];
    $reduced_values = $operator->reduceValueOptions($input_options);

    $this->assertSame(['foo', 'baz']array_keys($reduced_values));
    $this->assertInstanceOf(TranslatableMarkup::class$reduced_values['baz']);
    $this->assertSame('qux', (string) $reduced_values['baz']);
    $this->assertSame('bar', $reduced_values['foo']);

  }

}
Home | Imprint | This part of the site doesn't use cookies.