TextFieldBuilder.php in Diff 8
File
src/Plugin/diff/Field/TextFieldBuilder.php
View source
<?php
namespace Drupal\diff\Plugin\diff\Field;
use Drupal\diff\FieldDiffBuilderBase;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
class TextFieldBuilder extends FieldDiffBuilderBase {
public function build(FieldItemListInterface $field_items) {
$result = array();
foreach ($field_items as $field_key => $field_item) {
$values = $field_item
->getValue();
if ($this->configuration['compare_format'] == 1) {
if (isset($values['format'])) {
$controller = $this->entityTypeManager
->getStorage('filter_format');
$format = $controller
->load($values['format']);
$label = $this
->t('Format');
if ($format != NULL) {
$result[$field_key][] = $label . ": " . $format
->label();
}
else {
$result[$field_key][] = $label . ": " . $this
->t('Missing format @format', array(
'@format' => $values[$field_key],
));
}
}
}
if (isset($values['value'])) {
$value_only = TRUE;
if ($this->configuration['compare_format'] == 1) {
$value_only = FALSE;
}
$label = $this
->t('Value');
if ($value_only) {
$result[$field_key][] = $values['value'];
}
else {
$result[$field_key][] = $label . ":\n" . $values['value'];
}
}
}
return $result;
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['compare_format'] = array(
'#type' => 'checkbox',
'#title' => $this
->t('Compare format'),
'#default_value' => $this->configuration['compare_format'],
'#description' => $this
->t('This is only used if the "Text processing" instance settings are set to <em>Filtered text (user selects text format)</em>.'),
);
return parent::buildConfigurationForm($form, $form_state);
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['compare_format'] = $form_state
->getValue('compare_format');
parent::submitConfigurationForm($form, $form_state);
}
public function defaultConfiguration() {
$default_configuration = array(
'compare_format' => 0,
);
$default_configuration += parent::defaultConfiguration();
return $default_configuration;
}
}