Required.php in Tamper 8
File
src/Plugin/Tamper/Required.php
View source
<?php
namespace Drupal\tamper\Plugin\Tamper;
use Drupal\Core\Form\FormStateInterface;
use Drupal\tamper\Exception\SkipTamperItemException;
use Drupal\tamper\TamperableItemInterface;
use Drupal\tamper\TamperBase;
class Required extends TamperBase {
const SETTING_INVERT = 'invert';
protected $multiple = FALSE;
public function defaultConfiguration() {
$config = parent::defaultConfiguration();
$config[self::SETTING_INVERT] = FALSE;
return $config;
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form[self::SETTING_INVERT] = [
'#type' => 'checkbox',
'#title' => $this
->t('Invert filter'),
'#default_value' => $this
->getSetting(self::SETTING_INVERT),
'#description' => $this
->t('Inverting the filter will save items only if the field is empty.'),
];
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
$this
->setConfiguration([
self::SETTING_INVERT => (bool) $form_state
->getValue(self::SETTING_INVERT),
]);
}
public function tamper($data, TamperableItemInterface $item = NULL) {
$this->multiple = is_array($data);
$invert = $this
->getSetting(self::SETTING_INVERT);
if (!$invert && empty($data)) {
throw new SkipTamperItemException('Item is empty.');
}
if ($invert && !empty($data)) {
throw new SkipTamperItemException('Item is not empty.');
}
return $data;
}
public function multiple() {
return $this->multiple;
}
}
Classes
Name |
Description |
Required |
Plugin implementation for required values. |