You are here

class SetValueCommand in Config Pages 8.3

Same name and namespace in other branches
  1. 8.2 src/Command/SetValueCommand.php \Drupal\config_pages\Command\SetValueCommand

Class SetValueCommand.

@DrupalCommand ( extension="config_pages", extensionType="module" )

Hierarchy

  • class \Drupal\config_pages\Command\SetValueCommand extends \Drupal\Console\Core\Command\Command

Expanded class hierarchy of SetValueCommand

1 string reference to 'SetValueCommand'
console.services.yml in ./console.services.yml
console.services.yml
1 service uses SetValueCommand
config_pages.config_pages_set_value in ./console.services.yml
Drupal\config_pages\Command\SetValueCommand

File

src/Command/SetValueCommand.php, line 23

Namespace

Drupal\config_pages\Command
View source
class SetValueCommand extends Command {

  /**
   * @var \Drupal\Core\Logger\LoggerChannelInterface
   */
  protected $logger;

  /**
   * Constructs a new SetValueCommand object.
   * @param LoggerChannelFactoryInterface $logger_factory
   */
  public function __construct(LoggerChannelFactoryInterface $logger_factory) {
    $this->logger = $logger_factory
      ->get('config_pages');
    parent::__construct();
  }

  /**
   * {@inheritdoc}
   */
  protected function configure() {
    $this
      ->setName('config_pages:set_value')
      ->setDescription($this
      ->trans('commands.config_pages.set_value.description'))
      ->addArgument('bundle', InputArgument::REQUIRED, $this
      ->trans('commands.user.login.url.options.bundle'), NULL)
      ->addArgument('field_name', InputArgument::REQUIRED, $this
      ->trans('commands.user.login.url.options.field_name'), NULL)
      ->addArgument('value', InputArgument::REQUIRED, $this
      ->trans('commands.user.login.url.options.value'), NULL)
      ->addArgument('context', InputArgument::OPTIONAL, $this
      ->trans('commands.user.login.url.options.context'), NULL)
      ->setAliases([
      'cpsfv',
    ]);
  }

  /**
   * {@inheritdoc}
   */
  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());
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SetValueCommand::$logger protected property
SetValueCommand::configure protected function
SetValueCommand::execute protected function
SetValueCommand::__construct public function Constructs a new SetValueCommand object.