Sprintf.php in Tamper 8
File
src/Plugin/Tamper/Sprintf.php
View source
<?php
namespace Drupal\tamper\Plugin\Tamper;
use Drupal\Core\Form\FormStateInterface;
use Drupal\tamper\Exception\TamperException;
use Drupal\tamper\TamperableItemInterface;
use Drupal\tamper\TamperBase;
class Sprintf extends TamperBase {
const SETTING_TEXT_FORMAT = 'format';
public function defaultConfiguration() {
$config = parent::defaultConfiguration();
$config[self::SETTING_TEXT_FORMAT] = '%s';
return $config;
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form[self::SETTING_TEXT_FORMAT] = [
'#type' => 'textfield',
'#title' => $this
->t('Format'),
'#default_value' => $this
->getSetting(self::SETTING_TEXT_FORMAT),
'#size' => 60,
'#maxlength' => 128,
'#required' => TRUE,
'#description' => $this
->t('See the <a href="@url">sprintf</a> documentation for more details.', [
'@url' => 'http://www.php.net/manual/en/function.sprintf.php',
]),
];
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
$this
->setConfiguration([
self::SETTING_TEXT_FORMAT => $form_state
->getValue(self::SETTING_TEXT_FORMAT),
]);
}
public function tamper($data, TamperableItemInterface $item = NULL) {
if (!is_string($data)) {
throw new TamperException('Input should be a string.');
}
return sprintf($this
->getSetting(self::SETTING_TEXT_FORMAT), $data);
}
}
Classes
Name |
Description |
Sprintf |
Plugin implementation of the Sprintf plugin. |