class BlazyTextFormatter in Blazy 7
Same name and namespace in other branches
- 8.2 src/Plugin/Field/FieldFormatter/BlazyTextFormatter.php \Drupal\blazy\Plugin\Field\FieldFormatter\BlazyTextFormatter
Plugin implementation of the 'Blazy Text' formatter to have a grid of texts.
Hierarchy
- class \Drupal\blazy\Plugin\Field\FieldFormatter\FormatterBase implements FormatterInterface
- class \Drupal\blazy\Plugin\Field\FieldFormatter\BlazyTextFormatter uses BlazyFormatterTrait
Expanded class hierarchy of BlazyTextFormatter
File
- src/
Plugin/ Field/ FieldFormatter/ BlazyTextFormatter.php, line 12
Namespace
Drupal\blazy\Plugin\Field\FieldFormatterView source
class BlazyTextFormatter extends FormatterBase {
use BlazyFormatterTrait;
/**
* Constructs a BlazyTextFormatter instance.
*/
public function __construct($plugin_id, $field, $instance, BlazyManagerInterface $formatter) {
parent::__construct($plugin_id, $field, $instance);
$this->formatter = $formatter;
}
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return BlazyDefault::baseSettings();
}
/**
* {@inheritdoc}
*/
public function getRequiredForms() {
return [
'grid' => $this->isMultiple,
];
}
/**
* {@inheritdoc}
*/
public function admin() {
if (!isset($this->admin)) {
$this->admin = new BlazyAdminFormatter($this->formatter);
}
return $this->admin;
}
/**
* {@inheritdoc}
*/
public function viewElements($items, $entity) {
$settings = $this
->buildSettings();
$settings['_grid'] = !empty($settings['style']) && !empty($settings['grid']);
// Hand over to default formatter if not multiple as it is for grid only.
// @todo figure out to disable formatter like isApplicable() at D8 in the
// first place, and remove this.
if (!$this->isMultiple || !$settings['_grid']) {
$types = field_info_field_types($this->fieldDefinition['type']);
$display['type'] = isset($types['default_formatter']) ? $types['default_formatter'] : '';
if ($fallback = text_field_formatter_view($this->entityType, $entity, $this->fieldDefinition, $this->fieldInstance, $this->langcode, $items, $display)) {
return $fallback;
}
}
$settings['vanilla'] = TRUE;
$settings['namespace'] = $settings['item_id'] = 'blazy';
// Build the settings.
$build = [
'settings' => $settings,
];
// Modifies settings before building elements.
$this->formatter
->preBuildElements($build, $items, $entity);
// The ProcessedText element already handles cache context & tag bubbling.
// @see \Drupal\filter\Element\ProcessedText::preRenderText()
foreach ($items as $item) {
$content = _text_sanitize($this->fieldInstance, $settings['langcode'], $item, 'value');
$build[] = [
'#markup' => $content,
];
unset($content);
}
// Pass to manager for easy updates to all Blazy formatters.
return $this->formatter
->build($build);
}
/**
* {@inheritdoc}
*/
public function settingsForm($form, &$form_state, $definition) {
$element = [];
// @todo remove once D8-like isApplicable() for cardinality - 1 landed.
if (!$this->isMultiple) {
return $element;
}
$this
->admin()
->buildSettingsForm($element, $definition);
return $element;
}
/**
* Defines the scope for the form elements.
*/
public function getScopedFormElements() {
return [
'grid_required' => $this->isMultiple,
'no_image_style' => TRUE,
'no_layouts' => TRUE,
'responsive_image' => FALSE,
'style' => TRUE,
] + parent::getScopedFormElements();
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
BlazyFormatterTrait:: |
protected | property | The blazy-related formatter service. | |
BlazyFormatterTrait:: |
protected | property | The blazy field formatter manager. | |
BlazyFormatterTrait:: |
public | function | Returns the blazy-related formatter. | |
BlazyFormatterTrait:: |
public | function | Returns the blazy service. | |
BlazyFormatterTrait:: |
public | function | ||
BlazyTextFormatter:: |
public | function | ||
BlazyTextFormatter:: |
public static | function |
Returns default settings. Overrides FormatterBase:: |
|
BlazyTextFormatter:: |
public | function |
Returns required form elements for the current formatter. Overrides FormatterBase:: |
|
BlazyTextFormatter:: |
public | function |
Defines the scope for the form elements. Overrides FormatterBase:: |
|
BlazyTextFormatter:: |
public | function |
Implements hook_field_formatter_settings_form(). Overrides FormatterBase:: |
|
BlazyTextFormatter:: |
public | function |
Implements hook_field_formatter_view(). Overrides FormatterBase:: |
|
BlazyTextFormatter:: |
public | function |
Constructs a BlazyTextFormatter instance. Overrides FormatterBase:: |
|
FormatterBase:: |
protected | property | The blazy-related admin formatter service. | |
FormatterBase:: |
protected | property | Whether default settings have been merged into the current $settings. | |
FormatterBase:: |
protected | property | The field definition. | |
FormatterBase:: |
protected | property | The field display. | |
FormatterBase:: |
protected | property | The field instance. | |
FormatterBase:: |
protected | property | The form settings. | |
FormatterBase:: |
protected | property | True if the field cardinality equals -1. | |
FormatterBase:: |
protected | property | The known hard-coded entities. | |
FormatterBase:: |
protected | property | The langcode. | |
FormatterBase:: |
protected | property | The blazy formatter plugin id. | |
FormatterBase:: |
protected | property | The formatter settings. | |
FormatterBase:: |
protected | property | The view mode. | |
FormatterBase:: |
public | function | Builds the settings. | 1 |
FormatterBase:: |
public | function | Implements hook_field_formatter_settings_form(). | |
FormatterBase:: |
public | function | ||
FormatterBase:: |
public | function | Gets html settings. | |
FormatterBase:: |
public | function | Gets formatter plugin id. | |
FormatterBase:: |
public | function | ||
FormatterBase:: |
public | function | Gets formatter settings. | |
FormatterBase:: |
protected | function | Merges default settings values into $settings. | |
FormatterBase:: |
public | function | Sets html settings. | |
FormatterBase:: |
public | function | ||
FormatterBase:: |
public | function | Sets formatter settings. | |
FormatterBase:: |
protected | function | Setup common variables across different hooks. | |
FormatterBase:: |
public | function | Implements hook_field_formatter_view(). |