Skip to content

Instantly share code, notes, and snippets.

@seutje
Last active August 29, 2015 14:25
Show Gist options
  • Save seutje/3ca106bf0dde3cbb0990 to your computer and use it in GitHub Desktop.
Save seutje/3ca106bf0dde3cbb0990 to your computer and use it in GitHub Desktop.
Custom DS fields example (remove .php from filename, only for github code highlighter)
<?php
function somemodule_ds_fields_info($entity_type) {
$fields = array();
$fields['node']['some_field'] = array(
// Arbitrary UI label.
'title' => t('Some field'),
// DS constant for field type function.
'field_type' => DS_FIELD_TYPE_FUNCTION,
// Name of the actual function.
'function' => 'somemodule_some_field',
// Optional file the function resides in.
// 'file' => drupal_get_path('module', 'somemodule') . '/includes/ds.fields.inc',
// Limits on availability. Replace "node_type" with the type of node and "view_mode" with the view mode,
// wildcards(*) are available.
'ui_limit' => array(
'some_type|some_viewmode',
),
);
// I *always* forget this one.
return $fields;
}
function somemodule_somefield($field) {
// Store reference to entity (usually a node).
$entity = $field['entity'];
// Setup some holder arrays.
$output = array();
$content = array();
$field1 = array();
// Get an array of all values for the "telefoon" field.
$field1 = field_get_items($field['entity_type'], $entity, 'field_telefoon');
// Loop over the values
foreach ($field1 as $delta => $tel) {
// Safety because PHP is stupid.
if (isset($tel['value'])) {
// create simplest renderable array and stick it in our holder array.
$output[$delta] = array('#markup' => '<span class="telefoon">' . $tel['value'] . '</span>');
}
}
// Render our holder array and store in content delta 1.
$content[0] = render($output);
// Reset holder array.
$output = array();
$field2 = array();
// Get an array of all values for the "email" field.
$field2 = field_get_items($field['entity_type'], $entity, 'field_email');
// Loop-di-doop.
foreach ($field2 as $delta => $email) {
if (isset($email['value'])) {
// Same simple renderable array.
$output[$delta] = array('#markup' => '<span class="mail">' . $email['value'] . '</span>');
}
}
// Put second field in content delta 2.
$content[1] = array('#markup' => render($output));
// Render the whole content and return the markups.
return render($content);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment