Sunday, 29 July 2012

WordPress learning from the code in

My knowledge of WordPress is increasing quickly with little bits of time here and there, and I think I have finally found some functionality that will require me to develop a proper plugin, there doesn't seem to be a plugin that gives the same functionality of node references in Drupal (although that appears to be giving way to more generic entity references. Basically I want to add another post type and make reference to it from normal posts. I have to trawl through the plugins some more but so far nothing does exactly what I want.

So far I have a single plugin for the site that is doing a few simple things that some people would put in the theme templates (but that didn't feel right). Also making some decisions about what to put in the meta description tag.

With a system like Drupal or WordPress is is common to start learning from the admin interface inwards, which although it seems natural is a mistake in my opinion if you have other programming experience. I found it much easier to start from empty templates and work inwards, using documentation, core code and other examples for inspiration. I am wishing that, and looking I have been introduced to Drupal that way.

Obviously still a lot to learn but once I have developed a proper plugin I will probably have ticked enough boxes to start work on my more major language learning project.

Monday, 16 July 2012

WordPress from the ground up

Introduction

I have been putting some time aside to learning WordPress recently, in the the past I have had to do little bits and pieces on WordPress sites or a fiddle with something that doesn't quite work on a site built by someone else on top of a specialized theme that has been purchased. I wanted to learn how WordPress works from the bottom up.

I also want to get a feel for how much customization is possible and the feasibility of using WordPress as a development framework. I have a personal project in mind that theoretically I could build in Drupal, however Drupal is a bit weighty for what I want and a WordPress based project should be much easier for people to set up and install.

The site

The best way to learn is by doing, I built a theme and site from scratch, starting with empty files. Despite being graphically challenged and design challenged (I am clearly not a graphic designer ;)) I want to do a bit more of this type of thing just to understand the process and the challenges etc. Originally I was aiming to produce a responsive layout but that just added too much to the mix, the mechanics are one thing but working out how to design for it (I did mention I am not a designer at at all didn't I?), working in an unfamiliar environment etc. was took much in the time I had.

The next problem was content, normally I have been using Blogger for blogging, if I want to write something I just write it and not being commercially orientated, or traffic dependent it doesn't take up too much of my attention. Now I still haven't enough content to structure the blog the way I want to.

The blog is about the Raspberry Pi and my experiences with it at raspberry-fool.com Still lots to do, adding links, news items more static pages, Disqus comments etc. but I will just do a little every now and again a build it up. I am taking the time to play/experiment with plugins etc., working out how core code works and how the whole picture fits together.

I am fully aware that I could get a better WordPress blog up and polished off in less time, picking one of the many themes available, working through one of lists of recommended plugins. But then I would be working from the outside in, and learning a lot less.

Some observations

WordPress has more potential than I expected, soon I hope to find something I want to do that requires me to write a plugin (I have been reading about the process and investigating code but got beyond the theme) and then I will know for sure, but looks like I can build my application on to it.

.

Right now I am preferring the hook system in WordPress over Drupal, you can register and de-register functions against hooks.

Not sure whether I prefer it or not but interesting that in WordPress if I don't provide a template for something, in many cases nothing is displayed. There are some defaults but not so many as Drupal. At least this quickly shows me the areas where I need to work on something.

Thursday, 12 July 2012

Themeing Drupal 7: Depends on the team

Another steep learning Curve.

A quick comment, following some discussion with other Drupal developers and interested parties last week at the last Bristol & The West, UK Drupal group meetup. It quickly became clear that my search for simplicity in themeing Drupal is ultimately doomed. I know too many ways to do things, there are too many valid approaches.

The consensus appears to be that the skill-set and availability of members in the team will make a huge difference. Having a front-end developer that does not know Drupal inevitably moves a lot of the load back to the back-end developer (or at least introduces a heavy training/knowledge sharing penalty).

Although I can imagine a happier world where the developer is mostly concerned with making sure that the data is there and managing the display tab and view modes in content types, there are lots of times where this is simply not practical.

I shouldn't need a helper module to give me clues about where various bits of the page are being themed. I may have to trace theme functions in the code or trawl through a disparate set of template files that individually give no clue to their heirachy or conceptual inheritance.

Quite a few people seem to be looking forward to Twig in Drupal 8 another post on that soon I think.

Wednesday, 4 July 2012

Themeing Drupal 7: Checking fields

Introduction

A very general thought, will add more specifics when I have done a bit more investigation.

Considering the template end of themeing (so ignoring theme functions for this thought experiment) and restricting to a node template or field templates. Assuming you have have a set of various different fields (some with multiple values perhaps) and you wish to customise the display of the content somewhat perhaps wrapping some of the fields in html to make a carousel or for something else. Is there ever a case where you need write complex PHP in the template/s?

Templates can get nasty

Themeing Drupal is quite a big learning curve, in some organisations there is a clear division between developer and themer, between front-end and back-end development. Americans seem better at maintaining a clear division (although I don't have first-hand experience of this) whilst us Brits don't like to admit we can't do something so seem more likely to be blitting from server configuration, through back-end development, and onto front-end, admitting that you can't at least sort of do something is not easy.

Add tight deadlines to the mix, freelance Front End developers who don't know Drupal but who are used as a resource and before you know it your template files can be full of funky PHP pulling things out of $node to for display (even though they are available in $content) or even getting a reference to the node via an object reference in a field .....

Online I see questions about how to check if a field is empty in the node temple, with a variety of answers provided but never (or not in my experience) a question about why someone needs to do this.

Is it this simple?

Maybe not, in other frameworks typically by the time you get to the template level any variables you are interested will be available directly or via some sort of context. In the template a simple conditional check will allow you to determine what you need to display.

The solution for Drupal surely (please correct me if wrong) is to call render() on the field. You can check what is returned, if the the field is empty or doesn't exist the output of render will be empty. If not you can print it (wrapping it with any html etc.)

Yet again for a key field you can provide a field template. Putting the extras in that template and in the node template you can simply render the field knowing that if it empty nothing will be rendered.

There are of course theme functions and various places prior to the template where you can reach in and adjust the output, these are not immediately accessible to a front end developer without much Drupal experience and the back-end developer may well be too busy or they could be working remotely to each other.

So templates should be simple, free of complex PHP perhaps there are situations where this is not possible, I am investigating.

Tuesday, 3 July 2012

Drupal as framework pt2 where is my front controller?

Introduction

Continuing thoughts on Drupal7 (and to be fair probably just an exercise in tidying up my brain and not much use to others) as a web development framework, I highlighted one of the major difficulties in part 1 in that you need to know a lot about Drupal before you can use it as a framework.

This post is a quick pointer if you come from a pure framework background and are trying to understand how Drupal 7 works.

Please don't think I dislike Drupal

Some people get uncomfortable by anything but bouncy happy optimism, personally if the glass is half full I want to know how easy is it to get a refill. I will post about why I invest so much time in Drupal later.

Where is the controller?

Typically you may start looking for some sort of controller, some PHP code somewhere, that tells you how urls are mapped and dealt with. In many frameworks you can follow this chain through the source code and then start experimenting (break it to understand it). There are many ways to describe it and disagreements over details and terminology but something akin to a Front Controller is what you may expect to find. Looking for something like this is not a good initial approach to take with Drupal 7

In Drupal page requests are handled by index.php which contains just the following code:


<?php

/**
 * @file
 * The PHP page that serves all page requests on a Drupal installation.
 *
 * The routines here dispatch control to the appropriate handler, which then
 * prints the appropriate page.
 *
 * All Drupal code is released under the GNU General Public License.
 * See COPYRIGHT.txt and LICENSE.txt.
 */

/**
 * Root directory of Drupal installation.
 */
define('DRUPAL_ROOT', getcwd());

require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
menu_execute_active_handler();


The controller here is menu_execute_active_handler() the problem if you want to follow what happens afterwards is that there is no extended object or xml configuration or any sort of centralized user generated code file that you can find to understand how urls are mapped to actions (page callback functions). This function is a core function that ultimately gets its appropriate page callback function to execute from a database table called menu_router obtains its content from the action, allows any interested modules to hook in and add further content and finally renders the content to the browser (by default). The menu_router table will be mostly populated from a disparate collection of modules (both core, contributed and custom), implementing hook_menu.

My advice if you are trying to quickly get a handle on Drupal as a framework, is to understand hook_menu when I first started using Drupal the name put me off for a bit, something to do with menus? doesn't sound as important as it is but to quote from the documentation.

This hook enables modules to register paths in order to define how URL requests are handled.
more on hooks and the framework in a further post.

The following from DrupalconSF 2010 is a nice introduction to how content gets rendered in Drupal, Although bear in mind that some of the exact details may differ as it was quite early in Drupal 7 development.

If you want to see a built page just before it gets rendered then you can run a simple php script using drush scr to boostrap your application and run it.


<?php

$path = drupal_lookup_path('source','your/path');
echo "$path\n";

$page_result = menu_execute_active_handler($path,False);

print_r($page_result);
echo "\n";


What you will get should be the very large(usually) render array just before it is rendered in the to html (or possible something else). The drupal_lookup_path is required as the path you see is probably not the path Drupal uses due to path aliases and clean urls.

Sunday, 24 June 2012

Drupal bean module

Introduction

The Drupal Bean module makes blocks into fieldable entities, providing some useful UI features for blocks out of the box and allows developers to create new block types in code via ctools plugins. From the documentation:

"Each bean instance has a corresponding block. Users interact with them in the same way they might use any other block in Drupal. The Bean module does not attempt to interface with the placement of blocks. Bean deals only with the creation of customized, fieldable blocks."

I haven't tried Beans yet but can see how useful they could be, particularly in some places where views are overloaded. Filling bean content via EntityFieldQueries is a viable approach (and probably often better) to many problems that are solved with view blocks.

Further Reading

An interesting looking implementation of beans is the relevant content bean. This is feature in the following bean module tutorial:

The article Views without views discusses beans and this module also.

Friday, 8 June 2012

When buying and reading books just works

A quick post about Kindle, Amazon and something just working

Whilst thinking about another blog post I wanted to write I remembered a book I read a long time ago: The Computer Connection by Alfred Bester. I decided I wanted to read it again, hooked up my Kindle to company wireless, went online, purchased from Amazon and a few seconds later the book is ready for me to read on the bus journey home to-night.

Important to me is that:

  • I can choose to read it on my phone, Ipod touch or PC
  • My partner can read the same book on her kindle if she wishes
  • I can keep everything in sync across these devices
  • I can do the things I can do with a real book, but with extras added.
My book is not locked to one device, not keyed to scanning my retinas to ensure only I can ever read it or any other such non-sense. Amazon (and booksellers I presume) can make a profit without putting my reading experience into a straitjacket.