Abstracted array files (YAML?)

Events happening in the community are now at Drupal community events on www.drupal.org.
d00bz's picture

I am experimenting with abstracting the complex nested arrays that populate the definition of Drupal modules as separate resource files in YAML.

Example YAML for a menu: a separate file:


[module_dir]/resources/menu.yml

admin/game:
path: admin/game
title: game
page callback: game_admin
access arguments: [access administration pages]
type:
function: constant
arguments: [MENU_NORMAL_ITEM]

admin/game/add:
path: admin/game/add
title: Add Task
page callback: game_admin_add
access arguments: [access administration pages]
type:
function: constant
arguments: [MENU_NORMAL_ITEM]

admin/game/timeline:
path: admin/game/timeline
title: Timeline
page callback: game_admin_timeline
access arguments: [access administration pages]
type:
function: constant
arguments: [MENU_NORMAL_ITEM]

node/add/project:
path: node/add/project
title:
function: t
arguments: [Project]
access arguments: [create project]

------------------------ END -------------------------
You'll note that anything that is functional is broken down into a sub-array for post processing.

The advantage to a separate resource file for menus/nodes/forms is:

a) a cleaner leaner definition format for core elements
b) separation of key resources into files
c) lays the groundwork for a meta-system to edit module resources regardless of context.

potential costs:

a) The resources are interpreted which might cause some efficiency blowback. A solution would be to compile the resource file into a class or function as a mid step for efficiencies' sake.
b) adds another potential element to the learning curve of module creation.

The work I'm doing uses the Symfony YAML engine. Argument could be made to use the more popular JSON encoding to effect the same system -- there is nothing about YAML that makes it uniquely fitting for this purpose, it just has an exceptionally clean profile.

Comments

The YML -- formatted

d00bz's picture

Sorry -- the nesting of the YAML was flattened by the posting process. here is it, nested:

admin/game:
  path: admin/game
  title: game
  page callback: game_admin
  access arguments: [access administration pages]
  type: 
    function: constant
    arguments: [MENU_NORMAL_ITEM]
admin/game/add:
  path: admin/game/add
  title: Add Task
  page callback: game_admin_add
  access arguments: [access administration pages]
  type: 
    function: constant
    arguments: [MENU_NORMAL_ITEM]
admin/game/timeline:
  path: admin/game/timeline
  title: Timeline
  page callback: game_admin_timeline
  access arguments: [access administration pages]
  type: 
    function: constant
    arguments: [MENU_NORMAL_ITEM]
node/add/project:
  path: node/add/project
  title: 
    function: t
    arguments: [Project]
  access arguments: [create project]

another minor issue with yaml are the colons

stephen.moz's picture

Another issue with YAML is that you have to be careful if you are specifying a name value pair and the value contains a colon

YAML in module form

noobizness's picture

I have the YAML system in module form. When I've run it around the pike a few times I'll submit it to Drupal.

Hi, did anything ever come of

MichaelCole's picture

Hi, did anything ever come of this?