function atom_reference_field in Scald: Media Management made easy 6
Implements hook_field().
File
- atom_reference/
atom_reference.module, line 70 - Defines a new field type, allowing to directly reference Scald Atoms from a node.
Code
function atom_reference_field($op, &$node, $field, &$items, $teaser, $page) {
switch ($op) {
case 'validate':
// Ensure that the types of the referenced atoms match the one of those
// that were defined in the field configuration.
$types = $field['referencable_types'];
foreach ($items as $item) {
if (!$item['sid']) {
continue;
}
$atom = scald_fetch($item['sid']);
if (!isset($types[$atom->type]) || empty($types[$atom->type])) {
form_set_error('toto', t("Atom %title is of type %type, which can't be referenced in field %field", array(
'%title' => $atom->title,
'%type' => $atom->type,
'%field' => $field['widget']['label'],
)));
}
}
case 'sanitize':
// Get some safe values about the references atoms, checking that
// they still exist and are fetchable.
$sids = array();
foreach ($items as $delta => $item) {
if (is_array($item)) {
$items[$delta]['safe'] = array();
if (isset($item['sid'])) {
$atom = scald_fetch($item['sid']);
if (is_object($atom)) {
$items[$delta]['safe']['sid'] = $atom->sid;
$items[$delta]['safe']['title'] = check_plain($atom->title);
}
}
}
}
break;
}
}