You are here

public function BackgroundImageSettings::set in Background Image 2.0.x

Same name and namespace in other branches
  1. 8 src/BackgroundImageSettings.php \Drupal\background_image\BackgroundImageSettings::set()
  2. 2.x src/BackgroundImageSettings.php \Drupal\background_image\BackgroundImageSettings::set()

Sets a value in this configuration object.

Parameters

string $key: Identifier to store value in configuration.

mixed $value: Value to associate with identifier.

Return value

$this The configuration object.

Throws

\Drupal\Core\Config\ConfigValueException If $value is an array and any of its keys in any depth contains a dot.

Overrides ConfigBase::set

1 call to BackgroundImageSettings::set()
BackgroundImageSettings::merge in src/BackgroundImageSettings.php
Merges data into a configuration object.

File

src/BackgroundImageSettings.php, line 137

Class

BackgroundImageSettings

Namespace

Drupal\background_image

Code

public function set($key, $value) {

  // The schema wrapper depends on the most recent data structure to cast
  // values. It must be reset every time a value has changed to properly cast.
  $this->schemaWrapper = NULL;

  // Ensure all MarkupInterface objects are cast to strings.
  $value = $this
    ->castSafeStrings($value);

  // The dot/period is a reserved character; it may appear between keys, but
  // not within keys.
  if (is_array($value)) {
    $this
      ->validateKeys($value);
  }
  $parts = explode('.', $key);
  $nested = count($parts) > 1;
  if ($nested) {

    // For casting to work properly, the raw value must first be set.
    NestedArray::setValue($this->data, $parts, $value);

    // Now cast the value and set it again.
    $value = $this
      ->castValue($key, $value);
    NestedArray::setValue($this->data, $parts, $value);
  }
  else {

    // For casting to work properly, the raw value must first be set.
    $this->data[$key] = $value;

    // Now cast the value and set it again.
    $value = $this
      ->castValue($key, $value);
    $this->data[$key] = $value;
  }

  // Determine if value overrides original data.
  $overridden_value = NULL;
  if ($this->originalData) {
    $overridden_value = $value;
    $original_value = $this
      ->getOriginal($key);
    if (is_array($value) && is_array($original_value)) {
      if ($diff_value = DiffArray::diffAssocRecursive($value, $original_value)) {
        $overridden_value = $value;
      }
    }
    if (isset($overridden_value) && $overridden_value !== $original_value) {
      if ($nested) {
        NestedArray::setValue($this->overriddenData, $parts, $overridden_value);
      }
      else {
        $this->overriddenData[$key] = $overridden_value;
      }
    }
    elseif ($nested) {
      NestedArray::unsetValue($this->overriddenData, $parts);
    }
    else {
      unset($this->overriddenData[$key]);
    }
  }
  return $this;
}