You are here

public function ConfigEventSubscriber::onConfigSave in Fixed Block Content 8

Do the automatic default content export if default content has changed.

Parameters

\Drupal\Core\Config\ConfigCrudEvent $event: The config event.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

File

src/EventSubscriber/ConfigEventSubscriber.php, line 63

Class

ConfigEventSubscriber
An event subscriber that manages the auto-export feature.

Namespace

Drupal\fixed_block_content\EventSubscriber

Code

public function onConfigSave(ConfigCrudEvent $event) {
  $config = $event
    ->getConfig();
  $config_name = $config
    ->getName();

  // Work only with fixed block content configurations.
  if (strpos($config_name, 'fixed_block_content.fixed_block_content.') !== 0) {
    return;
  }

  // Work only if we are involved in a config import process, that is, the
  // config importer lock key is busy. We cannot relay on the
  // ConfigEvents::IMPORT event because we need to compare the updated
  // default_content with the original.
  if ($this->lock
    ->lockMayBeAvailable(ConfigImporter::LOCK_NAME)) {

    // The config importer is not locked, do nothing.
    return;
  }

  // Nothing to do if no auto-export option set.
  if (empty($config
    ->get('auto_export'))) {
    return;
  }

  /** @var \Drupal\fixed_block_content\FixedBlockContentInterface $fixed_block */
  $fixed_block = $this->entityTypeManager
    ->getStorage('fixed_block_content')
    ->load($config
    ->get('id'));
  if (!$fixed_block) {
    return;
  }

  // For any auto-export option, crete new block content if it doesn't exist.
  if (!$fixed_block
    ->getBlockContent(FALSE)) {
    $fixed_block
      ->exportDefaultContent();
  }
  elseif ($fixed_block
    ->getAutoExportState() == FixedBlockContentInterface::AUTO_EXPORT_ALWAYS && !empty($config
    ->get('default_content')) && $event
    ->isChanged('default_content')) {

    // The unconditional auto-export updates the existing block content.
    $fixed_block
      ->exportDefaultContent(TRUE);
  }
}