diff --git a/link.module b/link.module index a5b4f2a..df9ad49 100644 --- a/link.module +++ b/link.module @@ -52,6 +52,9 @@ function link_field_info() { ), 'default_widget' => 'link_field', 'default_formatter' => 'default', + // Support hook_entity_property_info() from contrib "Entity API". + 'property_type' => 'field_item_link', + 'property_callbacks' => array('link_field_property_info_callback'), ), ); } @@ -866,4 +869,54 @@ function link_content_migrate_instance_alter(&$instance_value, $field_value) { && $instance_value['widget']['type'] == 'link') { $instance_value['widget']['type'] = 'link_field'; } -} \ No newline at end of file +} + +/** + * Additional callback to adapt the property info of link fields. + * @see entity_metadata_field_entity_property_info(). + */ +function link_field_property_info_callback(&$info, $entity_type, $field, $instance, $field_type) { + $property = &$info[$entity_type]['bundles'][$instance['bundle']]['properties'][$field['field_name']]; + // Define a data structure so it's possible to deal with both the link title + // and URL. + $property['getter callback'] = 'entity_metadata_field_verbatim_get'; + $property['setter callback'] = 'entity_metadata_field_verbatim_set'; + + // Auto-create the field item as soon as a property is set. + $property['auto creation'] = 'link_field_item_create'; + + $property['property info'] = link_field_item_property_info(); + $property['property info']['url']['required'] = !$instance['settings']['url']; + $property['property info']['title']['required'] = ($instance['settings']['title'] == 'required'); + if ($instance['settings']['title'] == 'none') { + unset($property['property info']['title']); + } + + unset($property['query callback']); +} + +/** + * Callback for creating a new, empty link field item. + * + * @see link_field_property_info_callback() + */ +function link_field_item_create() { + return array('title' => NULL, 'url' => NULL); +} + +/** + * Defines info for the properties of the link-field item data structure. + */ +function link_field_item_property_info() { + $properties['title'] = array( + 'type' => 'text', + 'label' => t('The title of the link.'), + 'setter callback' => 'entity_property_verbatim_set', + ); + $properties['url'] = array( + 'type' => 'uri', + 'label' => t('The URL of the link.'), + 'setter callback' => 'entity_property_verbatim_set', + ); + return $properties; +}