CustomFormatters.php in Custom Formatters 8.3
File
src/Plugin/Derivative/CustomFormatters.php
View source
<?php
namespace Drupal\custom_formatters\Plugin\Derivative;
use Drupal\Component\Plugin\Derivative\DeriverBase;
class CustomFormatters extends DeriverBase {
protected $settings = [];
public function __construct() {
$this->settings = \Drupal::config('custom_formatters.settings');
}
public function getDerivativeDefinitions($base_plugin_definition) {
$formatters = \Drupal::entityTypeManager()
->getStorage('formatter')
->loadMultiple();
foreach ($formatters as $formatter) {
if ($formatter
->get('status')) {
$this->derivatives[$formatter
->id()] = $base_plugin_definition;
$this->derivatives[$formatter
->id()]['label'] = t($this
->getLabel($formatter
->label()));
$this->derivatives[$formatter
->id()]['field_types'] = $formatter
->get('field_types');
$this->derivatives[$formatter
->id()]['formatter'] = $formatter
->id();
$this->derivatives[$formatter
->id()]['config_dependencies'] = $formatter
->getDependencies();
$this->derivatives[$formatter
->id()]['config_dependencies']['config'][] = $formatter
->getConfigDependencyName();
}
}
return parent::getDerivativeDefinitions($base_plugin_definition);
}
protected function getLabel($label) {
if ($this->settings
->get('label_prefix')) {
$label = "{$this->settings->get('label_prefix_value')}: {$label}";
}
return $label;
}
}
Classes
Name |
Description |
CustomFormatters |
Retrieves field formatter plugin definitions for all custom formatters. |