With sincere gratitude from Webel IT Australia to Drupal CMS. Although it's not perfect, it is very powerful and is very popular with good reason.
The bad news is that Drupal8 still makes massive use of "by convention" structured arrays rather than full object-oriented encapsulation. It's still extremely Write Everything Twice (WET) (because "we enjoy typing") w.r.t. repetition of array keys, not DRY like proper OO code.
The good news is that this means that a version of OOE = Object Oriented Examples = One Of Each for Drupal8 may be welcomed by real OO advocates.
Consider for example this code from the Drupal8 examples project version 8.x-1.x-dev as of 02 Aug 2014:
namespace Drupal\page_example\Controller; use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; /** * Controller routines for page example routes. */ class PageExampleController { /** * Constructs a page with descriptive content. * * Our router maps this method to the path 'examples/page_example'. */ public function description() { $build = array( '#markup' => t('<p>The Page example module 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 router _content callback, maps the path 'examples/page_example/simple' * to this method. * * _content callbacks return a renderable array for the content area of the * page. The theme system will later render and surround the content with the * appropriate blocks, navigation, and styling. */ public 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. * The placeholder names "first" and "second" can have any value but should * match the callback method variable names; i.e. $first and $second. * * This function also demonstrates a more complex render array in the returned * values. Instead of rendering the HTML with theme('item_list'), content is * left un-rendered, and the theme function name is set using #theme. This * content will now 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. */ public 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; } }
That's one hell-of-a-lot of repetition of "by convention" array keys and some rather ugly error prone hand-written HTML code, including use of hand-written HTML tags. Yuck ! Compare that with the elegant render objects of OOE = Object Oriented Examples = One Of Each, where HTML tags are "wrapped" intelligently around low-level text to create valid Drupal structured render arrays for you. More powerful, more elegant, and less error prone. Simply better. Because it's real OO.
This educational site is brought to you by Webel IT Australia, experts in database-driven web technology for industry, engineering, education and science. Webel is one of Australia's most experienced Drupal CMS web site specialists.
'It ain't necessarily so,
It ain't necessarily so,
The t'ings dat yo' li'ble,
To read in de [Drupal6/7] Bible,
It ain't necessarily so.'
Heresy: Doctrine rejected as false by religious authorities.
Logical fallacy: Appeal to popularity, Argumentum ad populum.
© Copyright 2001 - 2016 Webel IT Australia (ABN: 67 677 268 579). All rights reserved (except as specified below).
PHP code examples from Webel IT Australia on this site are distributed under the GNU General Public License.
Excludes text and code snippets from Drupal.org quoted for educational purposes.
Drupal’s online documentation is © 2000-2014 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0.
PHP code from Drupal.org is distributed under the GNU General Public License.
Drupal® is a registered trademark of Dries Buytaert.
Text quoted from Wikipedia for educational purposes is made available under the Creative Commons Attribution-ShareAlike License.
Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc., a non-profit organization.
Site developed by Webel IT Australia.