function ViewExecutable::addHandler

Same name and namespace in other branches
  1. 9 core/modules/views/src/ViewExecutable.php \Drupal\views\ViewExecutable::addHandler()
  2. 8.9.x core/modules/views/src/ViewExecutable.php \Drupal\views\ViewExecutable::addHandler()
  3. 11.x core/modules/views/src/ViewExecutable.php \Drupal\views\ViewExecutable::addHandler()

Adds an instance of a handler to the view.

Items may be fields, filters, sort criteria, or arguments.

Parameters

string $display_id: The machine name of the display.

string $type: The type of handler being added.

string $table: The name of the table this handler is from.

string $field: The name of the field this handler is from.

array $options: (optional) Extra options for this instance. Defaults to an empty array.

string $id: (optional) A unique ID for this handler instance. Defaults to NULL, in which case one will be generated.

Return value

string The unique ID for this handler instance.

File

core/modules/views/src/ViewExecutable.php, line 2190

Class

ViewExecutable
Represents a view as a whole.

Namespace

Drupal\views

Code

public function addHandler($display_id, $type, $table, $field, $options = [], $id = NULL) {
    $types = $this::getHandlerTypes();
    $this->setDisplay($display_id);
    $data = $this->viewsData
        ->get($table);
    $fields = $this->displayHandlers
        ->get($display_id)
        ->getOption($types[$type]['plural']);
    if (empty($id)) {
        $id = $this->generateHandlerId($field, $fields);
    }
    // If the desired type is not found, use the original value directly.
    $handler_type = !empty($types[$type]['type']) ? $types[$type]['type'] : $type;
    $fields[$id] = [
        'id' => $id,
        'table' => $table,
        'field' => $field,
    ] + $options;
    if (isset($data['table']['entity type'])) {
        $fields[$id]['entity_type'] = $data['table']['entity type'];
    }
    if (isset($data[$field]['entity field'])) {
        $fields[$id]['entity_field'] = $data[$field]['entity field'];
    }
    // Load the plugin ID if available.
    if (isset($data[$field][$handler_type]['id'])) {
        $fields[$id]['plugin_id'] = $data[$field][$handler_type]['id'];
    }
    $this->displayHandlers
        ->get($display_id)
        ->setOption($types[$type]['plural'], $fields);
    return $id;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.