TestFieldEmptySettingFormatter.php in Zircon Profile 8.0
File
core/modules/field/tests/modules/field_test/src/Plugin/Field/FieldFormatter/TestFieldEmptySettingFormatter.php
View source
<?php
namespace Drupal\field_test\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
class TestFieldEmptySettingFormatter extends FormatterBase {
public static function defaultSettings() {
return array(
'field_empty_setting' => '',
) + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$element['field_empty_setting'] = array(
'#title' => t('Setting'),
'#type' => 'textfield',
'#size' => 20,
'#default_value' => $this
->getSetting('field_empty_setting'),
'#required' => TRUE,
);
return $element;
}
public function settingsSummary() {
$summary = array();
$setting = $this
->getSetting('field_empty_setting');
if (!empty($setting)) {
$summary[] = t('Default empty setting now has a value.');
}
return $summary;
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = array();
if (!empty($items)) {
foreach ($items as $delta => $item) {
$elements[$delta] = array(
'#markup' => $this
->getSetting('field_empty_setting'),
);
}
}
return $elements;
}
}