function ViewsHandlerManager::getHandler

Same name in other branches
  1. 9 core/modules/views/src/Plugin/ViewsHandlerManager.php \Drupal\views\Plugin\ViewsHandlerManager::getHandler()
  2. 8.9.x core/modules/views/src/Plugin/ViewsHandlerManager.php \Drupal\views\Plugin\ViewsHandlerManager::getHandler()
  3. 10 core/modules/views/src/Plugin/ViewsHandlerManager.php \Drupal\views\Plugin\ViewsHandlerManager::getHandler()

Fetches a handler from the data cache.

Parameters

array $item: An associative array representing the handler to be retrieved:

  • table: The name of the table containing the handler.
  • field: The name of the field the handler represents.

string|null $override_plugin_id: (optional) Override the actual handler object with this plugin ID. Used for aggregation when the handler is redirected to the aggregation handler.

Return value

\Drupal\views\Plugin\views\ViewsHandlerInterface An instance of a handler object. May be a broken handler instance.

File

core/modules/views/src/Plugin/ViewsHandlerManager.php, line 86

Class

ViewsHandlerManager
Plugin type manager for all views handlers.

Namespace

Drupal\views\Plugin

Code

public function getHandler(array $item, ?string $override_plugin_id = NULL) : ViewsHandlerInterface {
    $table = $item['table'];
    $field = $item['field'];
    // Get the plugin manager for this type.
    $data = $table ? $this->viewsData
        ->get($table) : $this->viewsData
        ->getAll();
    if (isset($data[$field][$this->handlerType])) {
        $definition = $data[$field][$this->handlerType];
        foreach ([
            'group',
            'title',
            'title short',
            'label',
            'help',
            'real field',
            'real table',
            'entity type',
            'entity field',
        ] as $key) {
            if (!isset($definition[$key])) {
                // First check the field level.
                if (!empty($data[$field][$key])) {
                    $definition[$key] = $data[$field][$key];
                }
                elseif (!empty($data['table'][$key])) {
                    $definition_key = $key === 'entity type' ? 'entity_type' : $key;
                    $definition[$definition_key] = $data['table'][$key];
                }
            }
        }
        // When aggregation is enabled, particular plugins need to be
        // replaced in order to override the query with a query that
        // can run the aggregate counts, sums, or averages for example.
        // @see Drupal\views\Plugin\views\query\Sql::getAggregationInfo()
        // for example which aggressively overrides any filter used
        // by a number of mathematical-type queries regardless of the
        // original filter.
        $plugin_id = $override_plugin_id ?: $definition['id'];
        // Try to use the overridden handler.
        $handler = $this->createInstance($plugin_id, $definition);
        if ($override_plugin_id && method_exists($handler, 'broken') && $handler->broken()) {
            $handler = $this->createInstance($definition['id'], $definition);
        }
        return $handler;
    }
    // Finally, use the 'broken' handler.
    return $this->createInstance('broken', [
        'original_configuration' => $item,
    ]);
}

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