ListFieldBuilder.php in Diff 8
File
src/Plugin/diff/Field/ListFieldBuilder.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 ListFieldBuilder extends FieldDiffBuilderBase {
public function build(FieldItemListInterface $field_items) {
$result = array();
foreach ($field_items as $field_key => $field_item) {
if (!$field_item
->isEmpty()) {
$possible_options = $field_item
->getPossibleOptions();
$values = $field_item
->getValue();
if ($this->configuration['compare']) {
switch ($this->configuration['compare']) {
case 'both':
$result[$field_key][] = $possible_options[$values['value']] . ' (' . $values['value'] . ')';
break;
case 'label':
$result[$field_key][] = $possible_options[$values['value']];
break;
default:
$result[$field_key][] = $values['value'];
break;
}
}
}
}
return $result;
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['compare'] = array(
'#type' => 'radios',
'#title' => $this
->t('Comparison method'),
'#options' => array(
'label' => $this
->t('Label'),
'key' => $this
->t('Key'),
'both' => $this
->t('Label (key)'),
),
'#default_value' => $this->configuration['compare'],
);
return parent::buildConfigurationForm($form, $form_state);
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['compare'] = $form_state
->getValue('compare');
parent::submitConfigurationForm($form, $form_state);
}
public function defaultConfiguration() {
$default_configuration = array(
'compare' => 'key',
);
$default_configuration += parent::defaultConfiguration();
return $default_configuration;
}
}