You are here

public function ConfigPagesCommands::pagesSetFieldValue in Config Pages 8.2

Same name and namespace in other branches
  1. 8.3 src/Commands/ConfigPagesCommands.php \Drupal\config_pages\Commands\ConfigPagesCommands::pagesSetFieldValue()

Set a value for the field of Config Pages.

@command config:pages-set-field-value

@option append Append to an existing value. @usage drush cpsfv bundle field_name value Set new value for field_name. @usage drush cpsfv bundle field_name value --append Append a value to existing string. @validate-module-enabled config_pages @aliases cpsfv,config-pages-set-field-value

Parameters

string $bundle: The type of config page "/admin/structure/config_pages/types".

string $field_name: The name of field.

string $value: The value for the field.

null $context: ConfigPage context.

array $options: An associative array of options whose values come from cli, aliases, config, etc.

File

src/Commands/ConfigPagesCommands.php, line 42

Class

ConfigPagesCommands
A Drush command file.

Namespace

Drupal\config_pages\Commands

Code

public function pagesSetFieldValue($bundle, $field_name, $value, $context = NULL, array $options = [
  'append' => NULL,
]) {
  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();
    }
    $append = $options['append'];
    if (isset($append)) {
      $value = $config_page
        ->get($field_name)
        ->getString() . $value;
    }
    $config_page
      ->set($field_name, str_replace('\\n', PHP_EOL, $value));
    $config_page
      ->save();
    $this
      ->output()
      ->writeln('Saved new value for ' . $field_name . ' field.');
  } catch (\Exception $e) {
    $this
      ->logger()
      ->error($e
      ->getMessage());
  }
}