You are here

class FormatterPlugin in Blazy 7

Blazy formatter plugin initializer.

Hierarchy

Expanded class hierarchy of FormatterPlugin

1 file declares its use of FormatterPlugin
blazy.runtime.inc in ./blazy.runtime.inc
Provides basic Blazy integration for lazy loading and multi-serving images.

File

src/Plugin/Field/FieldFormatter/FormatterPlugin.php, line 12

Namespace

Drupal\blazy\Plugin\Field\FieldFormatter
View source
class FormatterPlugin {
  use BlazyFormatterTrait;

  /**
   * The formatter info.
   *
   * @var array
   */
  protected $formatterInfo;

  /**
   * The formatter type instance.
   *
   * @var object
   */
  protected $formatterType;

  /**
   * Constructs a BlazyFormatter instance.
   */
  public function __construct(BlazyFormatter $formatter, BlazyManagerInterface $manager) {
    $this->formatter = $formatter;
    $this->manager = $manager;
  }

  /**
   * Implements hook_field_formatter_info().
   */
  public function formatterInfo() {
    if (!isset($this->formatterInfo)) {
      $formatters = [];
      foreach ([
        'file',
        'image',
        'text',
      ] as $type) {
        $class = 'Drupal\\blazy\\Plugin\\Field\\FieldFormatter\\Blazy' . ucwords($type) . 'Formatter';
        $formatters['blazy_' . $type] = [
          'label' => $type == 'text' ? t('Blazy grid') : t('Blazy'),
          'class' => $class,
          'field types' => $type == 'text' ? BlazyDefault::TEXTS : [
            $type,
          ],
          'settings' => $class::defaultSettings(),
        ];
      }
      $this->formatterInfo = $formatters;
    }
    return $this->formatterInfo;
  }

  /**
   * Return the cached formatter based on the field type.
   */
  public function getFormatter($type, $field, $instance, $namespace = 'blazy') {
    if (!isset($this->formatterType[$type])) {
      $this->formatterType[$type] = $this
        ->getActiveFormatter($type, $field, $instance, $namespace);
    }
    return $this->formatterType[$type];
  }

  /**
   * Return the uncached formatter based on the field type.
   */
  public function getActiveFormatter($type, $field, $instance, $namespace = 'blazy') {
    $plugin_id = $namespace . '_' . $type;
    foreach ($this
      ->formatterInfo() as $key => $info) {
      if ($key == $plugin_id && !empty($info['class'])) {
        $class = $info['class'];
        return new $class($plugin_id, $field, $instance, $this->formatter, $this->manager);
      }
    }
    return FALSE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BlazyFormatterTrait::$formatter protected property The blazy-related formatter service.
BlazyFormatterTrait::$manager protected property The blazy field formatter manager.
BlazyFormatterTrait::formatter public function Returns the blazy-related formatter.
BlazyFormatterTrait::manager public function Returns the blazy service.
BlazyFormatterTrait::settingsSummary public function
FormatterPlugin::$formatterInfo protected property The formatter info.
FormatterPlugin::$formatterType protected property The formatter type instance.
FormatterPlugin::formatterInfo public function Implements hook_field_formatter_info().
FormatterPlugin::getActiveFormatter public function Return the uncached formatter based on the field type.
FormatterPlugin::getFormatter public function Return the cached formatter based on the field type.
FormatterPlugin::__construct public function Constructs a BlazyFormatter instance.