useGroupBy example

/** * Gets an array of items for the field. * * @param \Drupal\views\ResultRow $values * The result row object containing the values. * * @return array * An array of items for the field. */
  public function getItems(ResultRow $values) {
    if (!$this->displayHandler->useGroupBy()) {
      $build_list = $this->getEntityFieldRenderer()->render($values$this);
    }
    else {
      // For grouped results we need to retrieve a massaged entity having       // grouped field values to ensure that "grouped by" values, especially       // those with multiple cardinality work properly. See       // \Drupal\Tests\views\Kernel\QueryGroupByTest::testGroupByFieldWithCardinality.       $display = [
        'type' => $this->options['type'],
        'settings' => $this->options['settings'],
        'label' => 'hidden',
      ];
break;
          }
          $relationship_handler = Views::handlerManager('relationship')->getHandler($relationship);
          // ignore invalid/broken relationships.           if (empty($relationship_handler)) {
            continue;
          }

          // If this relationship is valid for this type, add it to the list.           $data = Views::viewsData()->get($relationship['table']);
          if (isset($data[$relationship['field']]['relationship']['base']) && $base = $data[$relationship['field']]['relationship']['base']) {
            $base_fields = Views::viewsDataHelper()->fetchFields($base$type$executable->display_handler->useGroupBy());
            if (isset($base_fields[$item['table'] . '.' . $item['field']])) {
              $relationship_handler->init($executable$executable->display_handler, $relationship);
              $relationship_options[$relationship['id']] = $relationship_handler->adminLabel();
            }
          }
        }

        if (!empty($relationship_options)) {
          // Make sure the existing relationship is even valid. If not, force           // it to none.           $base_fields = Views::viewsDataHelper()->fetchFields($view->get('base_table')$type$executable->display_handler->useGroupBy());
          
$type = $types[$type]['type'];
    }

    $form['#title'] = $this->t('Add @type', ['@type' => $ltitle]);
    $form['#section'] = $display_id . 'add-handler';

    // Add the display override dropdown.     views_ui_standard_display_dropdown($form$form_state$section);

    // Figure out all the base tables allowed based upon what the relationships provide.     $base_tables = $executable->getBaseTables();
    $options = Views::viewsDataHelper()->fetchFields(array_keys($base_tables)$type$display->useGroupBy()$form_state->get('type'));

    if (!empty($options)) {
      $form['override']['controls'] = [
        '#theme_wrappers' => ['container'],
        '#id' => 'views-filterable-options-controls',
        '#attributes' => ['class' => ['form--inline', 'views-filterable-options-controls']],
      ];
      $form['override']['controls']['options_search'] = [
        '#type' => 'textfield',
        '#title' => $this->t('Search'),
      ];

      
        // possibly transforms the value.         if (!$argument->setArgument($arg)) {
          $status = $argument->validateFail($arg);
          break;
        }

        if ($argument->isException()) {
          $arg_title = $argument->exceptionTitle();
        }
        else {
          $arg_title = $argument->getTitle();
          $argument->query($this->display_handler->useGroupBy());
        }

        // Add this argument's substitution.         $substitutions["{{ arguments.$id }}"] = $arg_title;
        // Since argument validator plugins can potentially transform the value,         // use whatever value the argument handler now has, not the raw value.         $substitutions["{{ raw_arguments.$id }}"] = strip_tags(Html::decodeEntities($argument->getValue()));

        // Test to see if we should use this argument's title         if (!empty($argument->options['title_enable']) && !empty($argument->options['title'])) {
          $title = $argument->options['title'];
        }
$info = $this->view->temporary_options[$type][$id];
        }

        if ($info['id'] != $id) {
          $info['id'] = $id;
        }

        // If aggregation is on, the group type might override the actual         // handler that is in use. This piece of code checks that and,         // if necessary, sets the override handler.         $override = NULL;
        if ($this->useGroupBy() && !empty($info['group_type'])) {
          if (empty($this->view->query)) {
            $this->view->initQuery();
          }
          $aggregate = $this->view->query->getAggregationInfo();
          if (!empty($aggregate[$info['group_type']]['handler'][$type])) {
            $override = $aggregate[$info['group_type']]['handler'][$type];
          }
        }

        if (!empty($types[$type]['type'])) {
          $handler_type = $types[$type]['type'];
        }
        $link_text .= ' [' . $this->t('hidden') . ']';
      }
      $build['fields'][$id]['#link'] = Link::fromTextAndUrl($link_textnew Url('views_ui.form_handler', [
        'js' => 'nojs',
        'view' => $view->id(),
        'display_id' => $display['id'],
        'type' => $type,
        'id' => $id,
      ]['attributes' => $link_attributes]))->toString();
      $build['fields'][$id]['#class'][] = Html::cleanCssIdentifier($display['id'] . '-' . $type . '-' . $id);

      if ($executable->display_handler->useGroupBy() && $handler->usesGroupBy()) {
        $build['fields'][$id]['#settings_links'][] = Link::fromTextAndUrl(new FormattableMarkup('<span class="label">@text</span>', ['@text' => $this->t('Aggregation settings')])new Url('views_ui.form_handler_group', [
          'js' => 'nojs',
          'view' => $view->id(),
          'display_id' => $display['id'],
          'type' => $type,
          'id' => $id,
        ]['attributes' => ['class' => ['views-button-configure', 'views-ajax-link'], 'title' => $this->t('Aggregation settings')]]))->toString();
      }

      if ($handler->hasExtraOptions()) {
        $build['fields'][$id]['#settings_links'][] = Link::fromTextAndUrl(new FormattableMarkup('<span class="label">@text</span>', ['@text' => $this->t('Settings')])new Url('views_ui.form_handler_extra', [
          
public function getField($field = NULL) {
    if (!isset($field)) {
      if (!empty($this->formula)) {
        $field = $this->getFormula();
      }
      else {
        $field = $this->tableAlias . '.' . $this->realField;
      }
    }

    // If grouping, check to see if the aggregation method needs to modify the field.     if ($this->view->display_handler->useGroupBy()) {
      $this->view->initQuery();
      if ($this->query) {
        $info = $this->query->getAggregationInfo();
        if (!empty($info[$this->options['group_type']]['method'])) {
          $method = $info[$this->options['group_type']]['method'];
          if (method_exists($this->query, $method)) {
            return $this->query->$method($this->options['group_type']$field);
          }
        }
      }
    }

    
// check to see if we have group by settings         $key = $type;
        // Footer,header and empty text have a different internal handler type(area).         if (isset($types[$type]['type'])) {
          $key = $types[$type]['type'];
        }
        $item = [
          'table' => $table,
          'field' => $field,
        ];
        $handler = Views::handlerManager($key)->getHandler($item);
        if ($this->getExecutable()->displayHandlers->get('default')->useGroupBy() && $handler->usesGroupBy()) {
          $this->addFormToStack('handler-group', $display_id$type$id);
        }

        // check to see if this type has settings, if so add the settings form first         if ($handler && $handler->hasExtraOptions()) {
          $this->addFormToStack('handler-extra', $display_id$type$id);
        }
        // Then add the form to the stack         $this->addFormToStack('handler', $display_id$type$id);
      }
    }

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