You are here

class ConfigPagesCommands in Config Pages 8.3

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

A Drush command file.

See these files for an example of injecting Drupal services:

Hierarchy

Expanded class hierarchy of ConfigPagesCommands

1 string reference to 'ConfigPagesCommands'
drush.services.yml in ./drush.services.yml
drush.services.yml
1 service uses ConfigPagesCommands
config_pages.commands in ./drush.services.yml
\Drupal\config_pages\Commands\ConfigPagesCommands

File

src/Commands/ConfigPagesCommands.php, line 16

Namespace

Drupal\config_pages\Commands
View source
class ConfigPagesCommands extends DrushCommands {

  /**
   * Set a value for the field of Config Pages.
   *
   * @command config:pages-set-field-value
   * @param $bundle
   *   The type of config page "/admin/structure/config_pages/types"
   * @param $field_name
   *   The name of field
   * @param $value
   *   The value for the field
   * @param null $context
   * @param array $options An associative array of options whose values come from cli, aliases, config, etc.
   * @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
   */
  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());
    }
  }

  /**
   * Get a value for the field of Config Pages
   *
   * @param $bundle
   *   The type of config page "/admin/structure/config_pages/types"
   * @param $field_name
   *   The name of field
   * @param null $context
   * @validate-module-enabled config_pages
   *
   * @command config:pages-get-field-value
   * @aliases cpgfv,config-pages-get-field-value
   */
  public function pagesGetFieldValue($bundle, $field_name, $context = NULL) {
    try {
      $config_page = config_pages_config($bundle, $context);
      if (!empty($config_page)) {
        $this
          ->output()
          ->writeln($config_page
          ->get($field_name)->value);
      }
    } catch (\Exception $e) {
      $this
        ->logger()
        ->error($e
        ->getMessage());
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigPagesCommands::pagesGetFieldValue public function Get a value for the field of Config Pages
ConfigPagesCommands::pagesSetFieldValue public function Set a value for the field of Config Pages.