You are here

onlyone_admin_toolbar.module in Allow a content type only once (Only One) 8

File

modules/onlyone_admin_toolbar/onlyone_admin_toolbar.module
View source
<?php

/**
 * @file
 * Contains onlyone_admin_toolbar.module.
 */
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Url;

/**
 * Implements hook_help().
 */
function onlyone_admin_toolbar_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {

    // Main module help.
    case 'help.page.onlyone_admin_toolbar':
      $variables = [
        ':onlyone' => Url::fromRoute('help.page', [
          'name' => 'onlyone',
        ])
          ->toString(),
        ':admin-toolbar' => Url::fromRoute('help.page', [
          'name' => 'admin_toolbar_tools',
        ])
          ->toString(),
      ];
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('The Allow a content type only once (Only One) for Admin Toolbar module comes packaged with the <a href=":onlyone">Allow a content type only once (Only One)</a> module and handles modifications to the <a href=":admin-toolbar">Admin Toolbar Extra Tools</a> menu entries related to the configured content types.', $variables) . '</p>';
      $output .= '<h3>' . t('Uses') . '</h3>';
      $output .= '<p>' . t('To use Allow a content type only once (Only One) for Admin Toolbar just install it like any other module. There is no other configuration required. When a content type is configured to have Only One content and a content exists for this content type, the content type name will be shown with the text "(Edit)" at the end in the menu: Content » Add content.') . '</p>';
      return $output;
  }
}

/**
 * Implements hook_menu_links_discovered_alter().
 */
function onlyone_admin_toolbar_menu_links_discovered_alter(&$links) {

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

  // We need a new menu entry?
  $onlyone_new_menu_entry = \Drupal::config('onlyone.settings')
    ->get('onlyone_new_menu_entry');
  foreach ($onlyone_content_types as $content_type) {

    // Getting the nodes for the configured content types.
    $exists_nodes = \Drupal::service('onlyone')
      ->existsNodesContentType($content_type);

    // Adding the menu links if the new menu entry option is activate.
    if ($onlyone_new_menu_entry) {

      // Adding the link to 'Add content (Only One)
      $links['onlyone.add.' . $content_type] = [
        'route_name' => 'node.add',
        'route_parameters' => [
          'node_type' => $content_type,
        ],
        'parent' => 'onlyone.add_page',
      ];

      // Checking if we have nodes for the configured content type.
      if ($exists_nodes) {
        $links['onlyone.add.' . $content_type]['title'] = t('@content_type_name (Edit)', [
          '@content_type_name' => $links['node.add.' . $content_type]['title'],
        ]);
      }
      else {
        $links['onlyone.add.' . $content_type]['title'] = $links['node.add.' . $content_type]['title'];
      }

      // Deleting the link from 'Add content'.
      unset($links['node.add.' . $content_type]);
    }
    else {

      // If we have nodes we need to change the title.
      if ($exists_nodes) {
        $links['node.add.' . $content_type]['title'] = t('@content_type_name (Edit)', [
          '@content_type_name' => $links['node.add.' . $content_type]['title'],
        ]);
      }
    }
  }
}

/**
 * Implements hook_entity_insert().
 */
function onlyone_admin_toolbar_node_insert(EntityInterface $node) {

  // Rebuild the menu if needed.

  /** @var \Drupal\node\NodeInterface $node */
  \Drupal::service('onlyone.admin_toolbar')
    ->rebuildMenu($node
    ->getType());
}

/**
 * Implements hook_entity_delete().
 */
function onlyone_admin_toolbar_node_delete(EntityInterface $node) {

  // Rebuild the menu if needed.

  /** @var \Drupal\node\NodeInterface $node */
  \Drupal::service('onlyone.admin_toolbar')
    ->rebuildMenu($node
    ->getType());
}

/**
 * Implements hook_entity_update().
 */
function onlyone_admin_toolbar_node_update(EntityInterface $node) {

  // Rebuild the menu if needed.

  /** @var \Drupal\node\NodeInterface $node */
  \Drupal::service('onlyone.admin_toolbar')
    ->rebuildMenu($node
    ->getType());
}