Skip navigation.
Home
That which cannot be rendered in binary is by definition a delusion
 

Using YAML in Drupal: Configuration File Driven Drupal

YAML is a file format that delivers very spartan, DRY data structures, similar to JSON. While the point of this article is how to use YAML in Drupal, the same line of thought could be accomplished with JSON.

There are a lot of nested arrays in Drupal. Forms, Schema, Menus, and other similar strucutres. This kind of complex data in other systems tends to find its way to seperate config files. This cleans data out of code and makes it a little easier to maintain.

I am proposing here that it is easier to do a module with abstracted config files, and while we are at it, why not use a more compressed config structure like YAML?

Here is a snippet fo a schema in YAML:


function leads_schema() {
    return Drupal_YAML::load(drupal_get_path('module', 'leads') . '/schema.yml');
}

// YAML FILE BELOW:

--- 
leads_contact:
  description: A potential sales lead.
  primary key: [vid]
  fields:
    first_name:
      description: the legal first name of the contact
      type: varchar
      not_null: true
      length: 50
      default:
    last_name:
      description: the legal last name of the contact
      type: varchar
      not_null: true
      length: 50
      default:
    is_female:
    # 0 = male, 1 = female
      description: gender
      type: int
      not_null: true
      default: 1
    age:
      description: years old (at time of record creation).
      type: int
      unsigned: true
      not null: true
      default: 25
    vid:
      type: int
      unsigned: true
      not null: true
      default: 0
    nid:
      type: int
      unsigned: true
      not null: true
      default: 0
   ...

Here is that same schema in array format:

<?

function leads_schema(){
$schema = array(
        'leads_contact' => array(
                  'description' =>  'A potential sales lead.',
                'primary key' => array('vid'),
                'fields' => array(
                        'first_name' => array(
                                'description' =>  'the legal first name of the contact',
                                'type'=> 'varchar',
                                'not_null' => true,
                                'length' => 50,
                                'default' => ''),
                        'last_name' => array(
                        
                                'description' => 'the legal last name of the contact',
                                'type' => 'varchar',
                                'not_null' => true,
                                'length' => '50',
                                'default' => ''),
                        
// 0 = male, 1 = female
                        'is_female' => array(
                                'description' => 'gender',
                                'type' => 'int',
                                'not_null' => 'true',
                                'default' => '1')
                        ,
                        'age' => array(
                                'description' => 'years old (at time of record creation',
                                'type' => 'int',
                                'unsigned' => 'true',
                                'not null' => 'true',
                                'default' => 0),
                        
                        'vid' => array(
                                'type'=> 'int',
                                'unsigned'=> true,
                                'not null'=> true,
                                'default'=> 0),
                        'nid'=> array(
                        
                                'type'=> int,
                                'unsigned'=> true,
                                'not null'=> true,
                                'default'=> 0
                        )
                )
        )

...
);
return $schema;
}

Worst yet, the above is embedded in functions meaning it adds to parser bloat whether or not it is actually needed in a given context.

Menus are also nicely reducable to a YAML file. Forms not so much because they are internested with the node data.

AttachmentSize
yaml.zip128.84 KB

Post new comment

  • Allowed HTML tags: <a> <p> <span><small> <div> <h1> <h2> <h3> <h4> <h5> <h6> <img> <map> <area> <hr> <br> <br /> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <em> <b> <u> <i> <strong> <font> <del> <ins> <sub> <sup> <quote> <blockquote> <pre> <address> <code> <cite> <embed> <object> <param> <strike> <caption>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options