You are here

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

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

Callback for the onlyone-new-menu-entry command.

File

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

Code

function drush_onlyone_new_menu_entry() {
  $args = func_get_args();

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

  // Getting the values from the config.
  $onlyone_new_menu_entry = $config
    ->get('onlyone_new_menu_entry');

  // Giving colors to the messages.
  $activated = sprintf(OnlyOnePrintDrush::GREEN_OUTPUT, dt('Activated'));
  $disabled = sprintf(OnlyOnePrintDrush::RED_OUTPUT, dt('Disabled'));
  if (isset($args[0])) {

    // Assigning values to $value and $status.
    list($value, $status) = $args[0] == 'on' ? [
      1,
      strtolower($activated),
    ] : [
      0,
      strtolower($disabled),
    ];

    // Is already configured?
    if ($onlyone_new_menu_entry == $value) {

      // If is configured stop the command execution with a warning message.
      $message = dt('The option for shown the configured content type in a new menu entry is already @status.', [
        '@status' => $status,
      ]);
      drush_log($message, 'warning');
      return;
    }

    // Saving the values in the config.
    $config
      ->set('onlyone_new_menu_entry', $value);
    $config
      ->save();
    $message = dt('You have @status the option the show the configured content types in a new menu entry.', [
      '@status' => $status,
    ]);
    drush_log($message, 'success');

    // Rebuilding the routes.
    \Drupal::service('router.builder')
      ->rebuild();
  }
  else {

    // Defining the status.
    $status = $onlyone_new_menu_entry ? $activated : $disabled;
    $message = dt('The option to show the configured content types in a new menu entry is: @status', [
      '@status' => $status,
    ]);
    drush_print($message);
  }
}