jfu-1.0.x-dev/modules/jfu_usage/src/Controller/JfuUsageController.php
modules/jfu_usage/src/Controller/JfuUsageController.php
<?php
namespace Drupal\jfu_usage\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\EntityTypeBundleInfo;
use Drupal\Core\Entity\EntityFieldManager;
use Drupal\Core\Database\Connection;
use Drupal\Core\Link;
use Drupal\jfu\Services\JfuConfigServices;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides jfu usage route controllers.
*/
class JfuUsageController extends ControllerBase {
/**
* The database connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected $connection;
/**
* The jfu config services.
*
* @var \Drupal\jfu\Services\JfuConfigServices
*/
protected $jfuConfigServices;
/**
* The entity type bundle info.
*
* @var \Drupal\Core\Entity\EntityTypeBundleInfo
*/
protected $entityTypeBundleInfo;
/**
* The entity field manager.
*
* @var \Drupal\Core\Entity\EntityFieldManager
*/
protected $entityFieldManager;
/**
* The request stack.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected $requestStack;
/**
* Constructs a new JfuUsageController.
*
* @param \Drupal\Core\Database\Connection $connection
* The database connection.
* @param \Drupal\jfu\Services\JfuConfigServices $jfu_config_services
* The jfu config services.
* @param \Drupal\Core\Entity\EntityTypeBundleInfo $entity_type_bundle_info
* The entity type bundle info.
* @param \Drupal\Core\Entity\EntityTypeBundleInfo $entity_field_manager
* The entity field manager.
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* The request stack.
*/
public function __construct(Connection $connection, JfuConfigServices $jfu_config_services, EntityTypeBundleInfo $entity_type_bundle_info, EntityFieldManager $entity_field_manager, RequestStack $request_stack) {
$this->connection = $connection;
$this->jfuConfigServices = $jfu_config_services;
$this->entityTypeBundleInfo = $entity_type_bundle_info;
$this->entityFieldManager = $entity_field_manager;
$this->requestStack = $request_stack;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('database'),
$container->get('jfu_config.service'),
$container->get('entity_type.bundle.info'),
$container->get('entity_field.manager'),
$container->get('request_stack')
);
}
/**
*
* @return array
*/
public function list() {
$components = $this->jfuConfigServices->jfuListComponents();
$head = [
[
'data' => $this->t('Component name'),
],
[
'data' => $this->t('Usage'),
],
];
$rows = [];
foreach ($components as $component_type => $name) {
$component_use_fields = $this->componentUseField($component_type);
if (empty($component_use_fields)) {
continue;
}
$links = [];
foreach ($component_use_fields as $entity_type => $fields) {
$items = [];
foreach ($fields as $machine_name) {
$route_parameters = [
'component_type' => $component_type,
'entity_type' => $entity_type,
'field_machine_name' => $machine_name,
];
$items[] = Link::createFromRoute($this->t('Use in field - ' . $machine_name), 'jfu_usage.component_use', $route_parameters)->toRenderable();
}
$links[$component_type][$entity_type] = [
'#theme' => 'item_list',
'#title' => $this->t('Use in ' . $entity_type),
'#list_type' => 'ul',
'#items' => $items,
];
}
$row = [];
$row['data'][]['data'] = $name;
$row['data'][]['data'] = $links;
array_push($rows, $row);
}
$table = [
'#theme' => 'table',
'#header' => $head,
'#rows' => $rows,
];
return $table;
}
/**
*
* @return array
*/
public function componentUse($component_type, $entity_type, $field_machine_name) {
$params = $this->requestStack->getCurrentRequest()->query->all();
$entities = $this->loadEntities($entity_type, $field_machine_name, $params);
$head = [
[
'data' => $this->t('Entity title'),
],
[
'data' => $this->t('Entity type'),
],
[
'data' => $this->t('Link'),
],
];
$rows = [];
foreach ($entities as $entity_id) {
$id = $entity_id['entity_id'];
$entity = $this->entityTypeManager()->getStorage($entity_type)->load($id);
$name = $entity->label();
$row = [];
$row['data'][]['data'] = $name;
$row['data'][]['data'] = $entity_type;
$row['data'][]['data'] = Link::createFromRoute($name, 'entity.' . $entity_type . '.edit_form', [$entity_type => $id])->toRenderable();
array_push($rows, $row);
}
$results = [];
$results['#title'] = $this->t($component_type . ' Component usage');
$results['components']['filters'] = $this->formBuilder()->getForm('\Drupal\jfu_usage\Form\JfuUsageFiltersForm');
$results['entities']['table'] = [
'#type' => 'table',
'#header' => $head,
'#rows' => $rows,
'#empty' => $this->t('No entities found!'),
];
$results['entities']['pager'] = [
'#type' => 'pager',
];
return $results;
}
/**
*
* @param type $component_type
* @return type
*/
private function loadEntities($entity_type, $field_machine_name, $params) {
$rows_per_page = $this->config('jfu_usage.settings')->get('rows_per_page');
if (empty($rows_per_page)) {
$rows_per_page = 50;
}
$table = $entity_type . '__' . $field_machine_name;
$column = $field_machine_name . '_value';
$query = $this->connection->select($table, 'f');
$query->fields('f', ['entity_id']);
if (isset($params['key_value']) && !empty($params['key_value'])) {
$query->condition($column, '%' . $this->connection->escapeLike($params['key_value']) . '%', 'LIKE');
}
$query->groupBy('entity_id');
$pager = $query->extend('Drupal\Core\Database\Query\PagerSelectExtender')
->limit($rows_per_page);
$results = $pager->execute()->fetchAll(\PDO::FETCH_ASSOC);
return $results;
}
/**
*
* @param type $component_type
* @return type array
*/
private function componentUseField(string $component_type) {
$fields_json = $this->entityFieldManager->getFieldMapByFieldType('json_native');
$component_use_fields = [];
foreach ($fields_json as $entity_type => $fields) {
foreach ($fields as $machine_name => $field) {
$table = $entity_type . '__' . $machine_name;
$column = $machine_name . '_value';
$query = $this->connection->select($table, 'f');
$query->fields('f', ['entity_id']);
$query->condition($column, '%"' . $this->connection->escapeLike($component_type) . '"%', 'LIKE');
$query->groupBy('entity_id');
$query->range(0, 1);
$results = $query->execute()->fetchField();
if (!empty($results)) {
$component_use_fields[$entity_type][] = $machine_name;
}
}
}
return $component_use_fields;
}
}
