You are here

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

Same name and namespace in other branches
  1. 7 onlyone.module \onlyone_preprocess_node_add_list()

Implements hook_preprocess_HOOK() for block content add list templates.

File

./onlyone.module, line 110
Contains onlyone.module.

Code

function onlyone_preprocess_node_add_list(&$variables) {

  // Getting the route.
  $route = \Drupal::request()->attributes
    ->get('_route');

  // Getting variable to know if we should show the configured content type
  // in a new page.
  $onlyone_new_menu_entry = \Drupal::config('onlyone.settings')
    ->get('onlyone_new_menu_entry');

  // Leaving this code here until this issue will be resolved:
  // https://www.drupal.org/project/drupal/issues/2965718
  // @see https://www.drupal.org/project/onlyone/issues/3029598
  // Creating the message variable.
  if ($route == 'onlyone.add_page') {
    $variables['message'] = t('You have not configured any content type yet, go to the <a href=":config-content-types">Only One page</a> to configure the content types.', [
      ':config-content-types' => Url::fromRoute('onlyone.config_content_types')
        ->toString(),
    ]);
  }
  else {
    if ($onlyone_new_menu_entry) {
      $variables['message'] = t('All the content types are configured to have Only One node. Go to the <a href=":add-onlyone-content-type">Add content (Only One)</a> page to create or edit content.', [
        ':add-onlyone-content-type' => Url::fromRoute('onlyone.add_page')
          ->toString(),
      ]);
    }
    else {
      $variables['message'] = t('You have not created any content types yet. Go to the <a href=":add-content-type">Add content type</a> page to create content types.', [
        ':add-content-type' => Url::fromRoute('node.type_add')
          ->toString(),
      ]);
    }
  }

  // Getting the configured content types.
  $onlyone_content_types = \Drupal::config('onlyone.settings')
    ->get('onlyone_node_types');

  // Modifying the contet types information if needed.
  foreach ($onlyone_content_types as $content_type_machine_name) {

    // If the user don't have permission to create the content type even if it
    // is configured to have only one node the content type will not exists
    // in the render array.
    // See: https://www.drupal.org/project/onlyone/issues/3028525 .
    if (empty($variables['content'][$content_type_machine_name])) {
      continue;
    }

    // Getting help from the community to solve this part, see:
    // https://drupal.stackexchange.com/q/244030/28275 .
    // We need to modify the links?
    if (!($onlyone_new_menu_entry xor $route == 'onlyone.add_page')) {

      // Getting the content type.
      $content_type = $variables['content'][$content_type_machine_name];

      // Checking if exists nodes created for the content type.
      if (\Drupal::service('onlyone')
        ->existsNodesContentType($content_type_machine_name)) {

        // Assigning the new name.
        $new_name = t('@content_type_name (Edit)', [
          '@content_type_name' => $content_type
            ->get('name'),
        ]);
        $variables['content'][$content_type_machine_name]
          ->set('name', $new_name);

        // Changing the value in the original template, because all the themes
        // dont' use the same variables, see the seven theme as example.
        // @see template_preprocess_block_content_add_list().
        $variables['types'][$content_type_machine_name]['title'] = $new_name;
      }
      $original_description = empty($content_type
        ->getDescription()) ? '' : rtrim($content_type
        ->getDescription(), '.') . '.';
      $original_description_markup = Markup::create($original_description);
      $description = t('@description <strong>Only a single node can be created and edited</strong>.', [
        '@description' => $original_description_markup,
      ]);

      // Changing the value in the content type.
      $variables['content'][$content_type_machine_name]
        ->set('description', $description);

      // Changing the value in the original template, because all the themes
      // dont' use the same variables, see the seven theme as example.
      // @see template_preprocess_block_content_add_list().
      $variables['types'][$content_type_machine_name]['description'] = $description;
    }
    else {

      // We need to delete the links.
      unset($variables['content'][$content_type_machine_name]);
      unset($variables['types'][$content_type_machine_name]);
    }
  }
}