tamper-8.x-1.x-dev/src/Plugin/Tamper/Rewrite.php
src/Plugin/Tamper/Rewrite.php
<?php namespace Drupal\tamper\Plugin\Tamper; use Drupal\Core\Form\FormStateInterface; use Drupal\tamper\TamperBase; use Drupal\tamper\TamperableItemInterface; /** * Plugin implementation for rewriting a value. * * @Tamper( * id = "rewrite", * label = @Translation("Rewrite"), * description = @Translation("Rewrite a field using tokens."), * category = "Other" * ) */ class Rewrite extends TamperBase { const SETTING_TEXT = 'text'; /** * {@inheritdoc} */ public function defaultConfiguration() { $config = parent::defaultConfiguration(); $config[self::SETTING_TEXT] = ''; return $config; } /** * {@inheritdoc} */ public function buildConfigurationForm(array $form, FormStateInterface $form_state) { $form[self::SETTING_TEXT] = [ '#type' => 'textarea', '#title' => $this->t('Replacement pattern'), '#default_value' => $this->getSetting(self::SETTING_TEXT), ]; $replace = []; foreach ($this->sourceDefinition->getList() as $key => $label) { $replace[] = '[' . $key . ']'; } $form['help'] = [ '#type' => 'fieldset', '#title' => $this->t('Available replacement patterns'), 'list' => [ '#theme' => 'item_list', '#items' => $replace, ], ]; return $form; } /** * {@inheritdoc} */ public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { parent::submitConfigurationForm($form, $form_state); $this->setConfiguration([ self::SETTING_TEXT => $form_state->getValue(self::SETTING_TEXT), ]); } /** * {@inheritdoc} */ public function tamper($data, ?TamperableItemInterface $item = NULL) { if (is_null($item)) { // Nothing to rewrite. return $data; } $trans = []; foreach ($item->getSource() as $key => $value) { $trans['[' . $key . ']'] = is_array($value) ? reset($value) : $value; } return strtr($this->getSetting(self::SETTING_TEXT), $trans); } }