You are here

function rate_menu_local_tasks_alter in Rate 8.2

Same name and namespace in other branches
  1. 8 rate.module \rate_menu_local_tasks_alter()

Implements hook_menu_local_tasks_alter().

This unsets Voting Results tab for non-voting-enabled node types.

File

./rate.module, line 249
Hook implementation code for the Rate module.

Code

function rate_menu_local_tasks_alter(&$data, $route_name) {
  if (isset($data['tabs'][0]) && isset($data['tabs'][0]['entity.node.canonical'])) {
    $node = Drupal::request()->attributes
      ->get('node');
    if (!$node instanceof NodeInterface) {
      $node = Drupal::entityTypeManager()
        ->getStorage('node')
        ->load($node);
    }

    // Rate widgets settings - disable results if no widgets enabled on node.
    $entity_type_id = $node
      ->getEntityTypeId();
    $bundle = $node
      ->bundle();
    $widgets = \Drupal::service('entity_type.manager')
      ->getStorage('rate_widget')
      ->loadMultiple();
    if (!empty($widgets)) {
      $enabled_widgets = 0;
      foreach ($widgets as $widget => $widget_variables) {
        $entities = $widget_variables
          ->get('entity_types');
        $comments = $widget_variables
          ->get('comment_types');
        if ($entities && count($entities) > 0) {
          foreach ($entities as $id => $entity) {

            // Check if at least one widget is enabled.
            if ($entity == $entity_type_id . '.' . $bundle) {
              $enabled_widgets++;
            }
          }
        }
      }

      // Unset the results tab if no widgets linked to this node bundle.
      if ($enabled_widgets == 0) {
        unset($data['tabs'][0]['rate.node_results_page']);
      }
    }
  }
}