You are here

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

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

Callback for the onlyone-enable command.

File

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

Code

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

  // Getting the configured content types to have only one content.
  $onlyone_content_types = variable_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 = array();

  // 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
  // content (they are already configured to have Only One content).
  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, array(
      '@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.
    variable_set('onlyone_node_types', $onlyone_content_types);
    $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, array(
      '@names' => $names,
    ));
    drush_log($message, 'success');
  }
}