18.10.2016 Views

Drupal 7 Module Development

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Creating New Fields<br />

hook_field_extra_fields() lets us define, per entity type and per bundle,<br />

what "extra fields" the Field system should be aware of. In our case, we loop<br />

over all artwork types and declare that all of them have a title pseudo-field. That<br />

pseudo-field is present both when displaying the edit form for artwork entities<br />

(the first key) and when displaying them (the second key). In both cases the<br />

definition is the same but we could easily make them different if we want.<br />

There are only three keys to each definition. label and description should be<br />

self-explanatory. The weight key defines what the default "weight" of the title<br />

pseudo-field is in relation to other Fields (or pseudo-fields) on an entity. By setting<br />

the weight to -5, we ensure that by default the title will be shown first.<br />

There is no requirement that they stay there, however. Now that the Field system<br />

knows about the title field, the user can easily drag-and-drop the title to appear in<br />

between two different Fields, at the bottom of the page, or even not at all.<br />

Finding entities and fields<br />

Creating, editing, and deleting data is all well and good, and is a key part of any<br />

content management system. However, there is also another important feature that<br />

makes a CMS worthwhile–Searching.<br />

In a very simple case, searching for data is easy. We have a database, we know<br />

SQL, so let's rock and roll. However, <strong>Drupal</strong> doesn't restrict data to living in an<br />

SQL database. In fact, with field storage engines a single entity could conceivably<br />

live in a variety of different places. Consider the case of an artwork that has<br />

pictures associated with it via a field that pulls images from Flickr, and additional<br />

background information from a third party collection management system. Simply<br />

resorting to SQL to find artworks matching some given criteria is not feasible, since<br />

two of those three data stores is not an SQL database. Moreover, we may not<br />

be able to predict the table structure of the one that is since <strong>Drupal</strong> creates its<br />

database dynamically.<br />

Fortunately, <strong>Drupal</strong> provides a unified query system for searching entities and fields.<br />

Although it does not allow for searching across different data stores, it does provide<br />

a data store agnostic way of searching for entities. (Searching across different data<br />

stores is an incredibly complex problem, usually best solved by indexing all data<br />

from various data stores into a single search index such as Apache Solr.)<br />

[ 206 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!