TypedDataCommands.php in Typed Data API enhancements 8
File
src/Commands/TypedDataCommands.php
View source
<?php
namespace Drupal\typed_data\Commands;
use Drush\Commands\DrushCommands;
class TypedDataCommands extends DrushCommands {
public function listEntities() {
$entities = array_keys(\Drupal::entityTypeManager()
->getDefinitions());
$unique = array_unique($entities);
sort($unique);
$this
->output()
->writeln(dt('Entity machine names:'));
$this
->output()
->writeln(' ' . implode(PHP_EOL . ' ', $unique) . PHP_EOL);
}
public function listContexts() {
$contexts = array_keys(\Drupal::service('context.repository')
->getAvailableContexts());
$unique = array_unique($contexts);
sort($unique);
$this
->output()
->writeln(dt('Global context variables:'));
$this
->output()
->writeln(' ' . implode(PHP_EOL . ' ', $unique) . PHP_EOL);
}
public function listDataTypes() {
$definitions = \Drupal::service('typed_data_manager')
->getDefinitions();
$datatypes = [];
foreach ($definitions as $plugin) {
$datatypes[] = $plugin['id'];
}
$unique = array_unique($datatypes);
sort($unique);
$this
->output()
->writeln(dt('Available TypedData data types:'));
$this
->output()
->writeln(' ' . implode(PHP_EOL . ' ', $unique) . PHP_EOL);
}
public function listDataFilters() {
$this
->formatOutput('plugin.manager.typed_data_filter', 'Available TypedDataFilter plugins:', FALSE);
}
public function listFormWidgets() {
$this
->formatOutput('plugin.manager.typed_data_form_widget', 'Available TypedDataFormWidget plugins:', FALSE);
}
protected function formatOutput($plugin_manager_service, $title, $categories = TRUE, $short = FALSE) {
$definitions = \Drupal::service($plugin_manager_service)
->getDefinitions();
$plugins = [];
foreach ($definitions as $plugin) {
if ($categories) {
if ($short) {
$plugins[(string) $plugin['category']][] = $plugin['id'];
}
else {
$plugins[(string) $plugin['category']][] = $plugin['label'] . ' (' . $plugin['id'] . ')';
}
}
else {
if ($short) {
$plugins[] = $plugin['id'];
}
else {
$plugins[] = $plugin['label'] . ' (' . $plugin['id'] . ')';
}
}
}
$this
->output()
->writeln(dt($title));
if ($categories) {
ksort($plugins);
foreach ($plugins as $category => $plugin_list) {
$this
->output()
->writeln(' ' . $category);
sort($plugin_list);
$this
->output()
->writeln(' ' . implode(PHP_EOL . ' ', $plugin_list));
$this
->output()
->writeln('');
}
}
else {
$unique = array_unique($plugins);
sort($unique);
$this
->output()
->writeln(' ' . implode(PHP_EOL . ' ', $unique) . PHP_EOL);
}
}
}