You are here

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

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

Implements hook_preprocess_HOOK() for block content add list templates.

File

./onlyone.module, line 160
Allows to define if a content type must have more than one node in the site.

Code

function onlyone_preprocess_node_add_list(&$variables) {

  // Getting the configured content types to have only one node.
  $onlyone_content_types = variable_get('onlyone_node_types');

  // Getting the current path.
  $current_path = current_path();

  // Getting variable to know if we should show the configured content type
  // in a new page.
  $onlyone_new_menu_entry = variable_get('onlyone_new_menu_entry');

  // Creating the message variable.
  if ($current_path == 'onlyone/add') {
    $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.', array(
      '@config-content-types' => url('admin/config/content/onlyone'),
    ));
  }
  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.', array(
        '@add-onlyone-content-type' => url('onlyone/add'),
      ));
    }
    else {
      $variables['message'] = t('You have not created any content types yet. Go to the <a href="@create-content">Add content type</a> page to create content types.', array(
        '@create-content' => url('admin/structure/types/add'),
      ));
    }
  }

  // Loading the helper functions file.
  module_load_include('inc', 'onlyone', 'onlyone.helpers');

  // Changing the title.
  foreach ($variables['content'] as $key => $element) {

    // Getting the content type name.
    $content_type = substr($element['href'], 9);

    // Checking if the content type is configured.
    if (in_array($content_type, $onlyone_content_types)) {

      // We need to modify the links?
      if (!($onlyone_new_menu_entry xor $current_path == 'onlyone/add')) {

        // Checking if exists nodes created for the content type.
        if (_onlyone_exists_nodes_content_type($content_type)) {

          // Getting the content type name.
          $content_type_name = $variables['content'][$key]['title'];

          // Assigning the new name.
          $variables['content'][$key]['title'] = t('@content_type_name (Edit)', array(
            '@content_type_name' => $content_type_name,
          ));
        }
        $original_description = empty($variables['content'][$key]['description']) ? '' : rtrim($variables['content'][$key]['description'], '.') . '.';
        $variables['content'][$key]['description'] = t('!description <strong>Only a single node can be created and edited</strong>.', array(
          '!description' => $original_description,
        ));
      }
      else {

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