You are here

class CustomFormatters in Custom Formatters 8.3

Same name in this branch
  1. 8.3 src/Plugin/Derivative/CustomFormatters.php \Drupal\custom_formatters\Plugin\Derivative\CustomFormatters
  2. 8.3 src/Plugin/Field/FieldFormatter/CustomFormatters.php \Drupal\custom_formatters\Plugin\Field\FieldFormatter\CustomFormatters

Retrieves field formatter plugin definitions for all custom formatters.

Hierarchy

Expanded class hierarchy of CustomFormatters

File

src/Plugin/Derivative/CustomFormatters.php, line 10

Namespace

Drupal\custom_formatters\Plugin\Derivative
View source
class CustomFormatters extends DeriverBase {
  protected $settings = [];

  /**
   * CustomFormatters constructor.
   */
  public function __construct() {
    $this->settings = \Drupal::config('custom_formatters.settings');
  }

  /**
   * {@inheritdoc}
   */
  public function getDerivativeDefinitions($base_plugin_definition) {
    $formatters = \Drupal::entityTypeManager()
      ->getStorage('formatter')
      ->loadMultiple();

    /** @var \Drupal\custom_formatters\FormatterInterface $formatter */
    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);
  }

  /**
   * Returns Formatter label with optional prefix.
   *
   * @param string $label
   *   Formatter label.
   *
   * @return string
   *   The Formatter label with optional prefix.
   */
  protected function getLabel($label) {

    // Label prefix.
    if ($this->settings
      ->get('label_prefix')) {
      $label = "{$this->settings->get('label_prefix_value')}: {$label}";
    }
    return $label;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CustomFormatters::$settings protected property
CustomFormatters::getDerivativeDefinitions public function Gets the definition of all derivatives of a base plugin. Overrides DeriverBase::getDerivativeDefinitions
CustomFormatters::getLabel protected function Returns Formatter label with optional prefix.
CustomFormatters::__construct public function CustomFormatters constructor.
DeriverBase::$derivatives protected property List of derivative definitions. 1
DeriverBase::getDerivativeDefinition public function Gets the definition of a derivative plugin. Overrides DeriverInterface::getDerivativeDefinition