Commit fc1c5974 authored by Marco Villegas's avatar Marco Villegas
Browse files

Issue #1882676 follow-up: Use new routing system on page_example module.

parent 32cade15
Loading
Loading
Loading
Loading
+95 −0
Changes for page_example/lib/Drupal/page_example/Controller/PageExampleController.php: 95 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

/**
 * @file
 * Contains \Drupal\page_example\Controller\PageExampleController.
 */

namespace Drupal\page_example\Controller;

use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;

/**
 * Controller routines for filter routes.
 */
class PageExampleController {

  /**
   * Displays a page with a descriptive page.
   *
   * Our router maps this method to the path 'examples/page_example'.
   */
  function description() {
    $build = array(
      '#markup' => t('<p>The page_example provides two pages, "simple" and "arguments".</p><p>The <a href="@simple_link">simple page</a> just returns a renderable array for display.</p><p>The <a href="@arguments_link">arguments page</a> takes two arguments and displays them, as in @arguments_link</p>', array('@simple_link' => url('examples/page_example/simple', array('absolute' => TRUE)), '@arguments_link' => url('examples/page_example/arguments/23/56', array('absolute' => TRUE))))
    );

    return $build;
  }

  /**
   * Constructs a simple page.
   *
   * The simple _content callback, mapped to the path
   * 'examples/page_example/simple'.
   *
   * _content callbacks return a renderable array with the content area of the
   * page. The theme system will later render and surround the content in the
   * appropriate blocks, navigation, and styling.
   *
   * @todo: Is this paragraph valid?
   * If you do not want to use the theme system (for example for outputting an
   * image or XML), just return the right response object.
   */
  function simple() {
    return array(
      '#markup' => '<p>' . t('Simple page: The quick brown fox jumps over the lazy dog.') . '</p>'
    );
  }

  /**
   * A more complex _content callback that takes arguments.
   *
   * This callback is mapped to the path
   * 'examples/page_example/arguments/{first}/{second}'.
   *
   * The arguments in brackets are passed to this callback from the page URL.
   *
   * This function also demonstrates a more complex render array in the returned
   * values. Instead of just rendering the HTML with a theme('item_list'), the
   * list is left unrendered, and a #theme attached to it so that it can be
   * rendered as late as possible, giving more parts of the system a chance to
   * change it if necessary.
   *
   * Consult @link http://drupal.org/node/930760 Render Arrays documentation
   * @endlink for details.
   *
   * @param string $first
   *   A string to use, should be a number.
   * @param string $second
   *   Another string to use, should be a number.
   *
   * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
   *   If the parameters are invalid.
   */
  function arguments($first, $second) {
    // Make sure you don't trust the URL to be safe! Always check for exploits.
    if (!is_numeric($first) || !is_numeric($second)) {
      // We will just show a standard "access denied" page in this case.
      throw new AccessDeniedHttpException();
    }

    $list[] = t("First number was @number.", array('@number' => $first));
    $list[] = t("Second number was @number.", array('@number' => $second));
    $list[] = t('The total was @number.', array('@number' => $first + $second));

    $render_array['page_example_arguments'] = array(
      // The theme function to apply to the #items
      '#theme' => 'item_list',
      // The list itself.
      '#items' => $list,
      '#title' => t('Argument Information'),
    );
    return $render_array;
  }
}
+6 −5
Changes for page_example/lib/Drupal/page_example/Tests/PageExampleTest.php: 6 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -98,13 +98,14 @@ function testPageExampleBasic() {
    $this->assertRaw(t('Second number was @number.', array('@number' => $second)), 'Second argument successfully verified.');
    $this->assertRaw(t('The total was @number.', array('@number' => $first + $second)), 'arguments content successfully verified.');

    // @fixme Revisit to readd if new routing system provides a way to do
    // inclomplete arguments call and we implement it on this example.
    // Verify incomplete argument call to arguments content.
    $this->drupalGet('examples/page_example/arguments/' . $first . '/');
    $this->assertText('provides two pages');

    // $this->drupalGet('examples/page_example/arguments/' . $first . '/');
    // $this->assertText('provides two pages');
    // Verify invalid argument call to arguments content.
    $this->drupalGet('examples/page_example/arguments/' . $first . '/' . $this->randomString());
    $this->assertResponse(403, 'Invalid argument for arguments content successfully verified');
    //$this->drupalGet('examples/page_example/arguments/' . $first . '/' . $this->randomString());
    //$this->assertResponse(403, 'Invalid argument for arguments content successfully verified');

    // Verify invalid argument call to arguments content.
    $this->drupalGet('examples/page_example/arguments/' . $this->randomString() . '/' . $second);
+8 −111
Changes for page_example/page_example.module: 8 added lines, 111 removed lines.
Original line number Diff line number Diff line
@@ -16,8 +16,6 @@
 * @see menu_example
 */

use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;

/**
 * Implements hook_help().
 *
@@ -43,7 +41,7 @@ function page_example_help($path, $arg) {

    case 'admin/help#page_example':
      // Help text for the admin section, using the module name in the path.
      return t('This is help text created in the page example\'s second case.');
      return t("This is help text created in the page example's second case.");
  }
}

@@ -71,133 +69,32 @@ function page_example_permission() {
/**
 * Implements hook_menu().
 *
 * Because hook_menu() registers URL paths for items defined by the function, it
 * is necessary for modules that create pages. Each item can also specify a
 * callback function for a given URL. The menu items returned here provide this
 * information to the menu system.
 *
 * We will define some menus, and their paths will be interpreted as follows:
 *
 * If the user accesses http://example.com/?q=examples/page_example/simple,
 * the menu system will first look for a menu item with that path. In this case
 * it will find a match, and execute page_example_simple().
 *
 * If the user accesses http://example.com/?q=examples/page_example/arguments,
 * the menu system will find no explicit match, and will fall back to the
 * closest match, 'examples/page_example', executing page_example_description().
 *
 * If the user accesses
 * http://example.com/?q=examples/page_example/arguments/1/2, the menu
 * system will first look for examples/page_example/arguments/1/2. Not finding
 * a match, it will look for examples/page_example/arguments/1/%. Again not
 * finding a match, it will look for examples/page_example/arguments/%/2.
 * Yet again not finding a match, it will look for
 * examples/page_example/arguments/%/%. This time it finds a match, and so will
 * execute page_example_arguments(1, 2). Since the parameters are passed to
 * the function after the match, the function can do additional checking or
 * make use of them before executing the callback function.
 * We will define some menu items, and their paths will be interpreted as follows:
 *
 * @see hook_menu()
 * @see menu_example
 */
function page_example_menu() {

  // This is the minimum information you can provide for a menu item. This menu
  // item will be created in the default menu, usually Navigation.
  // This menu item will be created in the default menu, usually Navigation.
  $items['examples/page_example'] = array(
    'title' => 'Page Example',
    'page callback' => 'page_example_description',
    'access callback' => TRUE,
    'route_name' => 'page_example_description',
    'expanded' => TRUE,
  );

  $items['examples/page_example/simple'] = array(
    'title' => 'Simple - no arguments',
    'page callback' => 'page_example_simple',
    'access arguments' => array('access simple page'),
    'route_name' => 'page_example_simple',
  );

  // By using the MENU_CALLBACK type, we can register the callback for this
  // path without the item appearing in the menu; the admin cannot enable the
  // item in the menu, either.
  //
  // Notice that 'page arguments' is an array of numbers. These will be
  // replaced with the corresponding parts of the menu path. In this case a 0
  // would be replaced by 'example', a 1 by 'page_example', and a 2 by
  // 'arguments.' 3 and 4 will be replaced by whatever the user provides.
  // These will be passed as arguments to the page_example_arguments() function.
  $items['examples/page_example/arguments/%/%'] = array(
    'page callback' => 'page_example_arguments',
    'page arguments' => array(3, 4),
    'access arguments' => array('access arguments page'),
    'type' => MENU_CALLBACK,
  );
  // Notice there is not a declaration entry for
  // 'examples/page_example/arguments/{first}/{second}' because that is only a
  // routing item, and not a menu item (which needs to appear on menu).

  return $items;
}

/**
 * Constructs a descriptive page.
 *
 * Our menu maps this function to the path 'examples/page_example'.
 */
function page_example_description() {
  return array('#markup' => t('<p>The page_example provides two pages, "simple" and "arguments".</p><p>The <a href="@simple_link">simple page</a> just returns a renderable array for display.</p><p>The <a href="@arguments_link">arguments page</a> takes two arguments and displays them, as in @arguments_link</p>', array('@simple_link' => url('examples/page_example/simple', array('absolute' => TRUE)), '@arguments_link' => url('examples/page_example/arguments/23/56', array('absolute' => TRUE)))));
}

/**
 * Constructs a simple page.
 *
 * The simple page callback, mapped to the path 'examples/page_example/simple'.
 *
 * Page callbacks return a renderable array with the content area of the page.
 * The theme system will later render and surround the content in the
 * appropriate blocks, navigation, and styling.
 *
 * If you do not want to use the theme system (for example for outputting an
 * image or XML), you should print the content yourself and not return anything.
 */
function page_example_simple() {
  return array('#markup' => '<p>' . t('Simple page: The quick brown fox jumps over the lazy dog.') . '</p>');
}

/**
 * A more complex page callback that takes arguments.
 *
 * This callback is mapped to the path 'examples/page_example/arguments/%/%'.
 *
 * The % arguments are passed in from the page URL. In our hook_menu
 * implementation we instructed the menu system to extract the last two
 * parameters of the path and pass them to this function as arguments.
 *
 * This function also demonstrates a more complex render array in the returned
 * values. Instead of just rendering the HTML with a theme('item_list'), the
 * list is left unrendered, and a #theme attached to it so that it can be
 * rendered as late as possible, giving more parts of the system a chance to
 * change it if necessary.
 *
 * Consult @link http://drupal.org/node/930760 Render Arrays documentation
 * @endlink for details.
 */
function page_example_arguments($first, $second) {
  // Make sure you don't trust the URL to be safe! Always check for exploits.
  if (!is_numeric($first) || !is_numeric($second)) {
    // We will just show a standard "access denied" page in this case.
    throw new AccessDeniedHttpException();
    return;  // We actually don't get here.
  }

  $list[] = t("First number was @number.", array('@number' => $first));
  $list[] = t("Second number was @number.", array('@number' => $second));
  $list[] = t('The total was @number.', array('@number' => $first + $second));

  $render_array['page_example_arguments'] = array(
    '#theme' => 'item_list',  // The theme function to apply to the #items
    '#items' => $list,  // The list itself.
    '#title' => t('Argument Information'),
  );
  return $render_array;
}
/**
 * @} End of "defgroup page_example".
 */
+34 −0
Changes for page_example/page_example.routing.yml: 34 added lines, 0 removed lines.
Original line number Diff line number Diff line
# In order to create pages is necessary to define routes for them. The
# following lines defines three of them for this module.
page_example_description:
  pattern: 'examples/page_example'
  defaults:
    _content: '\Drupal\page_example\Controller\PageExampleController::description'
  requirements:
    _access: 'TRUE'
# If the user accesses http://example.com/?q=examples/page_example/simple,
# the menu system will first look for a route with that path. In this case it
# will find a match, and execute the _content callback.
page_example_simple:
  pattern: 'examples/page_example/simple'
  defaults:
    _content: '\Drupal\page_example\Controller\PageExampleController::simple'
  requirements:
    _permission: 'access simple page'
# @fixme Update this comment.
# If the user accesses
# http://example.com/?q=examples/page_example/arguments/1/2, the routing
# system will first look for examples/page_example/arguments/1/2. Not finding
# a match, it will look for examples/page_example/arguments/1/%. Again not
# finding a match, it will look for examples/page_example/arguments/%/2.
# Yet again not finding a match, it will look for
# examples/page_example/arguments/%/%. This time it finds a match, and so will
# execute page_example_arguments(1, 2). Since the parameters are passed to
# the function after the match, the function can do additional checking or
# make use of them before executing the callback function.
page_example_arguments:
  pattern: 'examples/page_example/arguments/{first}/{second}'
  defaults:
    _content: '\Drupal\page_example\Controller\PageExampleController::arguments'
  requirements:
    _permission: 'access arguments page'