You are here

public function ConfigActionsId::doSave in Config Actions 8

Save data to the source.

Parameters

array $data:

Return value

bool TRUE if the data was saved.

Overrides ConfigActionsSourceBase::doSave

File

src/Plugin/ConfigActionsSource/ConfigActionsId.php, line 165

Class

ConfigActionsId
Plugin for config id from the active store.

Namespace

Drupal\config_actions\Plugin\ConfigActionsSource

Code

public function doSave($data) {
  $config_item = $this
    ->getConfigItem();
  if (empty($config_item)) {
    return FALSE;
  }
  else {
    if (empty($data)) {
      $this->messenger
        ->addMessage($this
        ->t('Deleted %name', array(
        '%name' => $this->sourceId,
      )));
      $config_item
        ->delete();
    }
    else {
      $config_item
        ->setData($data);

      // Save any related entity for this config.
      // Taken from ConfigInstaller::createConfiguration()
      if ($entity_type = $this->configManager
        ->getEntityTypeIdByName($this->sourceId)) {

        /** @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface $entity_storage */
        $entity_storage = $this->configManager
          ->getEntityTypeManager()
          ->getStorage($entity_type);

        // It is possible that secondary writes can occur during configuration
        // creation. Updates of such configuration are allowed.
        if ($this->configStorage
          ->exists($this->sourceId)) {
          $id = $entity_storage
            ->getIDFromConfigName($this->sourceId, $entity_storage
            ->getEntityType()
            ->getConfigPrefix());
          $entity = $entity_storage
            ->load($id);
          $this->messenger
            ->addMessage($this
            ->t('Updated %name', array(
            '%name' => $this->sourceId,
          )));

          /** @var \Drupal\Core\Config\Entity\ConfigEntityInterface $entity */
          $entity = $entity_storage
            ->updateFromStorageRecord($entity, $config_item
            ->get());
        }
        else {
          $this->messenger
            ->addMessage($this
            ->t('Created %name', array(
            '%name' => $this->sourceId,
          )));
          $entity = $entity_storage
            ->createFromStorageRecord($config_item
            ->get());
        }
        if ($entity
          ->isInstallable()) {
          $entity
            ->trustData()
            ->save();
        }
      }
      else {
        $this->messenger
          ->addMessage($this
          ->t('Updated %name', array(
          '%name' => $this->sourceId,
        )));
        $config_item
          ->save();
      }
    }
  }
  return TRUE;
}