From 4020361ecb3706aa9282aaa77db44862987b987e Mon Sep 17 00:00:00 2001
From: webchick <webchick@24967.no-reply.drupal.org>
Date: Sun, 6 Oct 2013 21:33:22 -0700
Subject: [PATCH] Issue #1972990 by pwolanin, Dean Reilly, dawehner, Jberges,
 dokumori, AjitS, tim.plunkett: Convert tracker_page() to a Controller.

---
 .../lib/Drupal/Core/Menu/LocalTaskDefault.php | 49 ++++++-------------
 .../Access/ViewOwnTrackerAccessCheck.php      | 38 ++++++++++++++
 .../Drupal/tracker/Controller/TrackerPage.php | 24 +++++++++
 .../tracker/Controller/TrackerUserRecent.php  | 25 ++++++++++
 .../tracker/Controller/TrackerUserTab.php     | 33 +++++++++++++
 .../tracker/Plugin/Menu/UserTrackerTab.php    | 48 ++++++++++++++++++
 core/modules/tracker/tracker.local_tasks.yml  | 10 ++++
 core/modules/tracker/tracker.module           | 42 ----------------
 core/modules/tracker/tracker.routing.yml      | 30 ++++++++++++
 core/modules/tracker/tracker.services.yml     |  5 ++
 .../Tests/Core/Menu/LocalTaskDefaultTest.php  |  3 +-
 11 files changed, 230 insertions(+), 77 deletions(-)
 create mode 100644 core/modules/tracker/lib/Drupal/tracker/Access/ViewOwnTrackerAccessCheck.php
 create mode 100644 core/modules/tracker/lib/Drupal/tracker/Controller/TrackerPage.php
 create mode 100644 core/modules/tracker/lib/Drupal/tracker/Controller/TrackerUserRecent.php
 create mode 100644 core/modules/tracker/lib/Drupal/tracker/Controller/TrackerUserTab.php
 create mode 100644 core/modules/tracker/lib/Drupal/tracker/Plugin/Menu/UserTrackerTab.php
 create mode 100644 core/modules/tracker/tracker.local_tasks.yml
 create mode 100644 core/modules/tracker/tracker.routing.yml
 create mode 100644 core/modules/tracker/tracker.services.yml

diff --git a/core/lib/Drupal/Core/Menu/LocalTaskDefault.php b/core/lib/Drupal/Core/Menu/LocalTaskDefault.php
index 67e0afda8625..1ba0c3e0e418 100644
--- a/core/lib/Drupal/Core/Menu/LocalTaskDefault.php
+++ b/core/lib/Drupal/Core/Menu/LocalTaskDefault.php
@@ -7,16 +7,13 @@
 
 namespace Drupal\Core\Menu;
 
-use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
 use Drupal\Core\Plugin\PluginBase;
-use Drupal\Core\Routing\RouteProviderInterface;
-use Symfony\Component\DependencyInjection\ContainerInterface;
 use Symfony\Component\HttpFoundation\Request;
 
 /**
  * Default object used for LocalTaskPlugins.
  */
-class LocalTaskDefault extends PluginBase implements LocalTaskInterface, ContainerFactoryPluginInterface {
+class LocalTaskDefault extends PluginBase implements LocalTaskInterface {
 
   /**
    * The route provider to load routes by name.
@@ -32,35 +29,6 @@ class LocalTaskDefault extends PluginBase implements LocalTaskInterface, Contain
    */
   protected $active = FALSE;
 
-  /**
-   * Constructs a \Drupal\system\Plugin\LocalTaskDefault object.
-   *
-   * @param array $configuration
-   *   A configuration array containing information about the plugin instance.
-   * @param string $plugin_id
-   *   The plugin_id for the plugin instance.
-   * @param array $plugin_definition
-   *   The plugin implementation definition.
-   * @param \Drupal\Core\Routing\RouteProviderInterface $route_provider
-   *   The route provider.
-   */
-  public function __construct(array $configuration, $plugin_id, array $plugin_definition, RouteProviderInterface $route_provider) {
-    $this->routeProvider = $route_provider;
-    parent::__construct($configuration, $plugin_id, $plugin_definition);
-  }
-
-  /**
-   * {@inheritdoc}
-   */
-  public static function create(ContainerInterface $container, array $configuration, $plugin_id, array $plugin_definition) {
-    return new static(
-      $configuration,
-      $plugin_id,
-      $plugin_definition,
-      $container->get('router.route_provider')
-    );
-  }
-
   /**
    * {@inheritdoc}
    */
@@ -73,7 +41,7 @@ public function getRouteName() {
    */
   public function getRouteParameters(Request $request) {
     $parameters = isset($this->pluginDefinition['route_parameters']) ? $this->pluginDefinition['route_parameters'] : array();
-    $route = $this->routeProvider->getRouteByName($this->getRouteName());
+    $route = $this->routeProvider()->getRouteByName($this->getRouteName());
     $variables = $route->compile()->getVariables();
 
     // Normally the \Drupal\Core\ParamConverter\ParamConverterManager has
@@ -158,4 +126,17 @@ public function getActive() {
     return $this->active;
   }
 
+  /**
+   * Returns the route provider.
+   *
+   * @return \Drupal\Core\Routing\RouteProviderInterface
+   *   The route provider.
+   */
+  protected function routeProvider() {
+    if (!$this->routeProvider) {
+      $this->routeProvider = \Drupal::service('router.route_provider');
+    }
+    return $this->routeProvider;
+  }
+
 }
diff --git a/core/modules/tracker/lib/Drupal/tracker/Access/ViewOwnTrackerAccessCheck.php b/core/modules/tracker/lib/Drupal/tracker/Access/ViewOwnTrackerAccessCheck.php
new file mode 100644
index 000000000000..f0ec5084152b
--- /dev/null
+++ b/core/modules/tracker/lib/Drupal/tracker/Access/ViewOwnTrackerAccessCheck.php
@@ -0,0 +1,38 @@
+<?php
+
+/**
+ * @file
+ * Contains Drupal\tracker\Access\ViewOwnTrackerAccessCheck.
+ */
+
+namespace Drupal\tracker\Access;
+
+use Drupal\Core\Access\StaticAccessCheckInterface;
+use Symfony\Component\Routing\Route;
+use Symfony\Component\HttpFoundation\Request;
+
+/**
+ * Access check for user tracker routes.
+ */
+class ViewOwnTrackerAccessCheck implements StaticAccessCheckInterface {
+
+  /**
+   * {@inheritdoc}
+   */
+  public function appliesTo() {
+    return array('_access_tracker_own_information');
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function access(Route $route, Request $request) {
+    // The user object from the User ID in the path.
+    $user = $request->attributes->get('user');
+    // @todo - $account should be passed in.
+    // The \Drupal\Core\Session\AccountInterface $account trying to access this.
+    $account = \Drupal::currentUser();
+    return $user && $account->isAuthenticated() && ($user->id() == $account->id());
+  }
+}
+
diff --git a/core/modules/tracker/lib/Drupal/tracker/Controller/TrackerPage.php b/core/modules/tracker/lib/Drupal/tracker/Controller/TrackerPage.php
new file mode 100644
index 000000000000..a9d667a4486d
--- /dev/null
+++ b/core/modules/tracker/lib/Drupal/tracker/Controller/TrackerPage.php
@@ -0,0 +1,24 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\tracker\Controller\TrackerPage.
+ */
+
+namespace Drupal\tracker\Controller;
+
+use Drupal\Core\Controller\ControllerBase;
+
+/**
+ * Controller for tracker.page route.
+ */
+class TrackerPage extends ControllerBase {
+
+  /**
+   * Content callback for the tracker.page route.
+   */
+  public function getContent() {
+    module_load_include('inc', 'tracker', 'tracker.pages');
+    return tracker_page();
+  }
+}
diff --git a/core/modules/tracker/lib/Drupal/tracker/Controller/TrackerUserRecent.php b/core/modules/tracker/lib/Drupal/tracker/Controller/TrackerUserRecent.php
new file mode 100644
index 000000000000..4b644aed50ae
--- /dev/null
+++ b/core/modules/tracker/lib/Drupal/tracker/Controller/TrackerUserRecent.php
@@ -0,0 +1,25 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\tracker\Controller\TrackerUserRecent.
+ */
+
+namespace Drupal\tracker\Controller;
+
+use Drupal\Core\Controller\ControllerBase;
+use Drupal\user\UserInterface;
+
+/**
+ * Controller for tracker.users_recent_content route.
+ */
+class TrackerUserRecent extends ControllerBase {
+
+  /**
+   * Content callback for the tracker.users_recent_content route.
+   */
+  public function getContent(UserInterface $user) {
+    module_load_include('inc', 'tracker', 'tracker.pages');
+    return tracker_page($user);
+  }
+}
diff --git a/core/modules/tracker/lib/Drupal/tracker/Controller/TrackerUserTab.php b/core/modules/tracker/lib/Drupal/tracker/Controller/TrackerUserTab.php
new file mode 100644
index 000000000000..cb595f75238c
--- /dev/null
+++ b/core/modules/tracker/lib/Drupal/tracker/Controller/TrackerUserTab.php
@@ -0,0 +1,33 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\tracker\Controller\TrackerUserTab.
+ */
+
+namespace Drupal\tracker\Controller;
+
+use Drupal\Core\Controller\ControllerBase;
+use Drupal\user\UserInterface;
+use Drupal\Component\Utility\String;
+
+/**
+ * Controller for tracker.user_tab route.
+ */
+class TrackerUserTab extends ControllerBase {
+
+  /**
+   * Content callback for the tracker.user_tab route.
+   */
+  public function getContent(UserInterface $user) {
+    module_load_include('inc', 'tracker', 'tracker.pages');
+    return tracker_page($user);
+  }
+
+  /**
+   * Title callback for the tracker.user_tab route.
+   */
+  public function getTitle(UserInterface $user) {
+    return String::checkPlain(user_format_name($user));
+  }
+}
diff --git a/core/modules/tracker/lib/Drupal/tracker/Plugin/Menu/UserTrackerTab.php b/core/modules/tracker/lib/Drupal/tracker/Plugin/Menu/UserTrackerTab.php
new file mode 100644
index 000000000000..3e195afe81b0
--- /dev/null
+++ b/core/modules/tracker/lib/Drupal/tracker/Plugin/Menu/UserTrackerTab.php
@@ -0,0 +1,48 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\tracker\Plugin\Menu\UserTrackerTab.
+ */
+
+namespace Drupal\tracker\Plugin\Menu;
+
+use Drupal\Core\Menu\LocalTaskDefault;
+use Symfony\Component\HttpFoundation\Request;
+
+/**
+ * Provides route parameters needed to link to the current user tracker tab.
+ */
+class UserTrackerTab extends LocalTaskDefault {
+
+  /**
+   * Current user object.
+   *
+   * @var \Drupal\Core\Session\AccountInterface
+   */
+  protected $currentUser;
+
+  /**
+   * Gets the current active user.
+   *
+   * @todo: https://drupal.org/node/2105123 put this method in
+   *   \Drupal\Core\Plugin\PluginBase instead.
+   *
+   * @return \Drupal\Core\Session\AccountInterface
+   */
+  protected function currentUser() {
+    if (!$this->currentUser) {
+      $this->currentUser = \Drupal::currentUser();
+    }
+    return $this->currentUser;
+  }
+
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getRouteParameters(Request $request) {
+    return array('user' => $this->currentUser()->Id());
+  }
+
+}
diff --git a/core/modules/tracker/tracker.local_tasks.yml b/core/modules/tracker/tracker.local_tasks.yml
new file mode 100644
index 000000000000..7600bd0b29d4
--- /dev/null
+++ b/core/modules/tracker/tracker.local_tasks.yml
@@ -0,0 +1,10 @@
+tracker.page_tab:
+  route_name: tracker.page
+  title: 'Recent content'
+  tab_root_id: tracker.page_tab
+
+tracker.users_recent_tab:
+  route_name: tracker.users_recent_content
+  title: 'My recent content'
+  tab_root_id: tracker.page_tab
+  class: '\Drupal\tracker\Plugin\Menu\UserTrackerTab'
diff --git a/core/modules/tracker/tracker.module b/core/modules/tracker/tracker.module
index 88f4f02f9a7e..878a755f4562 100644
--- a/core/modules/tracker/tracker.module
+++ b/core/modules/tracker/tracker.module
@@ -30,48 +30,6 @@ function tracker_help($path, $arg) {
   }
 }
 
-/**
- * Implements hook_menu().
- */
-function tracker_menu() {
-  $items['tracker'] = array(
-    'title' => 'Recent content',
-    'page callback' => 'tracker_page',
-    'access arguments' => array('access content'),
-    'weight' => 1,
-    'file' => 'tracker.pages.inc',
-  );
-  $items['tracker/all'] = array(
-    'title' => 'All recent content',
-    'type' => MENU_DEFAULT_LOCAL_TASK,
-  );
-  $items['tracker/%user_uid_optional'] = array(
-    'title' => 'My recent content',
-    'page callback' => 'tracker_page',
-    'access callback' => '_tracker_myrecent_access',
-    'access arguments' => array(1),
-    'page arguments' => array(1),
-    'type' => MENU_LOCAL_TASK,
-    'file' => 'tracker.pages.inc',
-  );
-
-  $items['user/%user/track'] = array(
-    'title' => 'Track',
-    'page callback' => 'tracker_page',
-    'page arguments' => array(1, TRUE),
-    'access callback' => '_tracker_user_access',
-    'access arguments' => array(1),
-    'type' => MENU_LOCAL_TASK,
-    'file' => 'tracker.pages.inc',
-  );
-  $items['user/%user/track/content'] = array(
-    'title' => 'Track content',
-    'type' => MENU_DEFAULT_LOCAL_TASK,
-  );
-
-  return $items;
-}
-
 /**
  * Implements hook_cron().
  *
diff --git a/core/modules/tracker/tracker.routing.yml b/core/modules/tracker/tracker.routing.yml
new file mode 100644
index 000000000000..4de8d8fe2499
--- /dev/null
+++ b/core/modules/tracker/tracker.routing.yml
@@ -0,0 +1,30 @@
+tracker.page:
+  path: '/tracker'
+  defaults:
+    _content: '\Drupal\tracker\Controller\TrackerPage::getContent'
+    _title: 'Recent content'
+  requirements:
+    _permission: 'access content'
+
+tracker.users_recent_content:
+  path: '/tracker/{user}'
+  defaults:
+    _content: '\Drupal\tracker\Controller\TrackerUserRecent::getContent'
+    _title: 'My recent content'
+  options:
+    _access_mode: 'ALL'
+  requirements:
+    _permission: 'access content'
+    _access_tracker_own_information: 'TRUE'
+
+tracker.user_tab:
+  path: '/user/{user}/track'
+  defaults:
+    _content: '\Drupal\tracker\Controller\TrackerUserTab::getContent'
+    _title_callback: '\Drupal\tracker\Controller\TrackerUserTab::getTitle'
+  options:
+    _access_mode: 'ALL'
+  requirements:
+    _permission: 'access content'
+    _entity_access: 'user.view'
+
diff --git a/core/modules/tracker/tracker.services.yml b/core/modules/tracker/tracker.services.yml
new file mode 100644
index 000000000000..e657c1f57978
--- /dev/null
+++ b/core/modules/tracker/tracker.services.yml
@@ -0,0 +1,5 @@
+services:
+  access_check.tracker.view_own:
+    class: Drupal\tracker\Access\ViewOwnTrackerAccessCheck
+    tags:
+      - { name: access_check }
diff --git a/core/tests/Drupal/Tests/Core/Menu/LocalTaskDefaultTest.php b/core/tests/Drupal/Tests/Core/Menu/LocalTaskDefaultTest.php
index 1777ab88220e..f7231bf35867 100644
--- a/core/tests/Drupal/Tests/Core/Menu/LocalTaskDefaultTest.php
+++ b/core/tests/Drupal/Tests/Core/Menu/LocalTaskDefaultTest.php
@@ -86,8 +86,9 @@ protected function setUp() {
   protected function setupLocalTaskDefault() {
     $container = new ContainerBuilder();
     $container->set('string_translation', $this->stringTranslation);
+    $container->set('router.route_provider', $this->routeProvider);
     \Drupal::setContainer($container);
-    $this->localTaskBase = new LocalTaskDefault($this->config, $this->pluginId, $this->pluginDefinition, $this->routeProvider);
+    $this->localTaskBase = new LocalTaskDefault($this->config, $this->pluginId, $this->pluginDefinition);
   }
 
   /**
-- 
GitLab