Drupal 7 field API

Embed Size (px)

Citation preview

Drupal 7: What's new

Karoly Negyesi

Clarity Digital (examiner.com)

What's new in Drupal 7

See http://blip.tv/file/2772239

Tons of new APIs

Most of them are well documented and easy

Field API: many new concepts and gotchas

Field API

Extremely powerful

API and not browser driven

Our work now is precision surgery

Key concepts

Bundles

Field storage

Field types

Widgets

Formatters

Fields

Attach

Instances

Anatomy of a node

Organization: node type

Primary identifier: nid

Version identifier: vid

Anatomy of a term

Organization: vocabulary

Primary identifier: tid

Version identifier: tid (same)

Anatomy of an entity

Entity type: node

Bundle: node type

Primary id: nid

Version id: vid

Type: taxonomy term

Bundle: vocabulary

Primary id: tid

Version id: tid

list($id, $vid, $bundle) = entity_extract_ids($obj_type, $object);

Field storage

Storage primitives

Cornerstone of scalability

MongoDB

Field type

Business logic of a field

Text, file, image...

Gotcha: extremely badly named hooks.
for eg. hook_field_load
node-like one only hook
should be field_type not field.

Field

A bunch of settings

Field type

Cardinality

Storage engine

All the things that are hard to upgrade

Core does not upgrade

Attaching

Attaching

(a) Field

(to a) Bundle

(is an) Instance

Instance

Again a bunch of settings

Widget settings

Formatter settings

Trivial to update

Gotcha: formatter settings are stored in 'display'

Difference

Fields describe data structure and behaviour

Hard to update

Instances are trivial to update

Instances are mostly just widget / formatter

Widgets

Data entry mechanism

Generic: textfield, textarea, options_select...

Specific: Image (but there is always an alter)

Formatter

Displays a field

Generic

Specific (yes, we have an alter here too)

Field attach API

field_attach_* called explicitly by entity modules:field_attach_presave('node', $node);

These call same named hooksmodule_invoke_all('field_attach_presave', $obj_type, $object);

hook arguments are entity type and the entity

Gotcha: does not deal with attaching a field

But at least it's a real hook.

Field attach API continued

Entity information can be extracted:

list($id, $vid, $bundle, $cacheable) = entity_extract_ids($obj_type, $object);

Instances can be found:

field_info_instances($obj_type, $bundle)

So all the gotchas

hook_field_X is really field_type X callback

Entity type variables are typically $obj_type

Similarly, entities are $object

Formatter settings are in $instance['display']

The field attach API deals with fields already attached aka. Instances and not the attach itself

Taxonomy specific gotchas

Vocabularies themselves are entities too

Taxonomy_term_reference

Hiearchy is not a field

Hook cornucopia

field_attach_update('user', $object);

user_module_invoke('update', $edit,
$user, $category);

entity_invoke('update', 'user', $user);

Click to edit the title text format

Muokkaa otsikon tekstimuotoa napsauttamalla

Muokkaa jsennyksen tekstimuotoa napsauttamallaToinen jsennystasoKolmas jsennystasoNeljs jsennystasoViides jsennystasoKuudes jsennystasoSeitsems jsennystasoKahdeksas jsennystasoYhdekss jsennystaso