You are here

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

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

Callback for the onlyone-disable command.

File

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

Code

function drush_onlyone_disable() {
  $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');

  // Deleting the values.
  $content_types_not_configured = [];
  foreach ($args as $content_type) {
    if (!in_array($content_type, $onlyone_content_types)) {
      $content_types_not_configured[] = $content_type;
    }
    else {
      \Drupal::service('onlyone')
        ->deleteContentTypeConfig($content_type);
    }
  }

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

  // Printing the not configured content types.
  $content_types_to_disable = array_diff($args, $content_types_not_configured);
  if (count($content_types_to_disable)) {
    $names = implode(', ', $content_types_to_disable);
    $singular = "In the following content type the 'Only One content' mode was disabled: @names";
    $plural = "In the following content types the 'Only One content' mode was disabled: @names";
    $cant = count($content_types_to_disable);
    $message = _onlyone_drush_plural($cant, $singular, $plural, [
      '@names' => $names,
    ]);
    drush_log($message, 'success');
  }
}