You are here

protected function SetValueCommand::execute in Config Pages 8.2

Same name and namespace in other branches
  1. 8.3 src/Command/SetValueCommand.php \Drupal\config_pages\Command\SetValueCommand::execute()

File

src/Command/SetValueCommand.php, line 78

Class

SetValueCommand
Class Drupal command.

Namespace

Drupal\config_pages\Command

Code

protected function execute(InputInterface $input, OutputInterface $output) {
  $bundle = $input
    ->getArgument('bundle');
  $field_name = $input
    ->getArgument('field_name');
  $value = $input
    ->getArgument('value');
  $context = $input
    ->getArgument('context');
  try {
    $config_page = config_pages_config($bundle, $context);
    if (empty($config_page)) {
      $type = ConfigPagesType::load($bundle);
      $config_page = ConfigPages::create([
        'type' => $bundle,
        'label' => $type
          ->label(),
        'context' => $type
          ->getContextData(),
      ]);
      $config_page
        ->save();
    }
    $config_page
      ->set($field_name, str_replace('\\n', PHP_EOL, $value));
    $config_page
      ->save();
    $output
      ->writeln('Saved new value for ' . $field_name . ' field.');
  } catch (\Exception $e) {
    $this->logger
      ->error($e
      ->getMessage());
  }
}