You are here

public function DefaultValue::tamper in Tamper 8

Tamper data.

Performs the operations on the data to transform it.

Parameters

mixed $data: The data to tamper.

\Drupal\tamper\TamperableItemInterface $item: Item that can be tampered as part of a plugin's execution.

Return value

mixed The tampered data.

Throws

\Drupal\tamper\Exception\TamperException When the plugin can not tamper the given data.

\Drupal\tamper\Exception\SkipTamperDataException When the calling tamper process should be skipped for the given data.

\Drupal\tamper\Exception\SkipTamperItemException When the calling tamper process should be skipped for the given item.

Overrides TamperInterface::tamper

File

src/Plugin/Tamper/DefaultValue.php, line 67

Class

DefaultValue
Plugin implementation for setting a value or default value.

Namespace

Drupal\tamper\Plugin\Tamper

Code

public function tamper($data, TamperableItemInterface $item = NULL) {

  // Setting a default value.
  $only_if_empty = $this
    ->getSetting(self::SETTING_ONLY_IF_EMPTY);
  if (!empty($only_if_empty) && !$data) {
    $data = $this
      ->getSetting(self::SETTING_DEFAULT_VALUE);
  }
  elseif (empty($only_if_empty)) {
    $data = $this
      ->getSetting(self::SETTING_DEFAULT_VALUE);
  }
  return $data;
}