class SetValueCommand in Config Pages 8.2
Same name and namespace in other branches
- 8.3 src/Command/SetValueCommand.php \Drupal\config_pages\Command\SetValueCommand
Class Drupal command.
@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'
1 service uses SetValueCommand
File
- src/
Command/ SetValueCommand.php, line 22
Namespace
Drupal\config_pages\CommandView source
class SetValueCommand extends Command {
/**
* Logger interface.
*
* @var \Drupal\Core\Logger\LoggerChannelInterface
*/
protected $logger;
/**
* Constructs a new SetValueCommand object.
*
* @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
* 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
SetValueCommand:: |
protected | property | Logger interface. | |
SetValueCommand:: |
protected | function | ||
SetValueCommand:: |
protected | function | ||
SetValueCommand:: |
public | function | Constructs a new SetValueCommand object. |