You are here

function drush_onlyone_enable in Allow a content type only once (Only One) 8

Same name and namespace in other branches
  1. 7 onlyone.drush.inc \drush_onlyone_enable()

Callback for the onlyone-enable command.

File

./onlyone.drush.inc, line 208
Drush commands related to the Only One module.

Code

function drush_onlyone_enable() {
  $args = array_unique(func_get_args());

  // Getting an editable config because we will get and set a value.
  $config = \Drupal::service('config.factory')
    ->getEditable('onlyone.settings');
  $onlyone_content_types = $config
    ->get('onlyone_node_types');

  // Array to store content types that are configured and don't need to be
  // configured one more time.
  $content_types_configured = [];

  // Adding the new values to save.
  foreach ($args as $content_type) {
    if (in_array($content_type, $onlyone_content_types)) {
      $content_types_configured[] = $content_type;
    }
    else {
      $onlyone_content_types[] = $content_type;
    }
  }

  // Printing the content types that will not be configured to have Only One
  // node (they are already configured to have Only One node).
  if (count($content_types_configured)) {
    $names = implode(', ', $content_types_configured);
    $singular = "The following content type is already configured in 'Only One content' mode: @names";
    $plural = "The following content types are already configured in 'Only One content' mode: @names";
    $cant = count($content_types_configured);
    $message = _onlyone_drush_plural($cant, $singular, $plural, [
      '@names' => $names,
    ]);
    drush_log($message, 'warning');
  }

  // Printing the configured content types.
  $content_types_to_enable = array_diff($args, $content_types_configured);
  if (count($content_types_to_enable)) {

    // Saving the values in the config.
    $config
      ->set('onlyone_node_types', $onlyone_content_types);
    $config
      ->save();
    $names = implode(', ', $content_types_to_enable);
    $singular = "The following content type was configured in 'Only One content' mode: @names";
    $plural = "The following content types were configured in 'Only One content' mode: @names";
    $cant = count($content_types_to_enable);
    $message = _onlyone_drush_plural($cant, $singular, $plural, [
      '@names' => $names,
    ]);
    drush_log($message, 'success');
  }
}