You are here

protected static function TargetingFormTrait::trimTargetingValues in Doubleclick for Publishers (DFP) 8

Helper function that removes empty targets form form values.

1 call to TargetingFormTrait::trimTargetingValues()
TargetingFormTrait::targetingFormValidate in src/Form/TargetingFormTrait.php
Validation function used by the targeting form.

File

src/Form/TargetingFormTrait.php, line 145
Contains \Drupal\dfp\Form\TargetingFormTrait.

Class

TargetingFormTrait
Adds a form for saving DFP targeting information.

Namespace

Drupal\dfp\Form

Code

protected static function trimTargetingValues(&$values, $parent = 'targeting') {
  foreach ($values as $key => &$val) {
    if ($key === $parent) {

      // We found the targeting values.
      foreach ($val as $k => $v) {
        if (empty($val[$k]['target']) && empty($val[$k]['value'])) {
          unset($val[$k]);
        }
      }

      // Reset the array indexes to prevent wierd behavior caused by a target
      // being removed in the middle of the array.
      $val = array_values($val);
      break;
    }
    elseif (is_array($val)) {
      self::trimTargetingValues($val, $parent);
    }
  }
}