1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | /** * Implements hook_field_info(). */ function link_field_info() { return array( 'link_field' => array( 'label' => t('Link'), 'description' => t('Store a title, href, and attributes in the database to assemble a link.'), // ... 'property_type' => 'field_item_link', 'property_callbacks' => array('link_field_property_info_callback'), ), ); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | /** * 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']]; $property['property info'] = array( 'title' => array( 'type' => 'text', 'label' => t('The title of the link.'), 'microdata' => TRUE, ), 'url' => array( 'type' => 'uri', 'label' => t('The URL of the link.'), 'microdata' => TRUE, ), ); if ($instance['settings']['title'] == 'none') { unset($property['property info']['title']); } } |
1 2 3 | <a href="http://drupal.org">Drupal</a> <a itemprop="url" href="http://drupal.org"><span itemprop="name">Drupal</span></a> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | /** * Implements hook_field_formatter_view(). */ function link_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) { $elements = array(); $microdata = array(); // If the microdata module is enabled, the microdata mapping will have been // passed in via the entity. if (module_exists('microdata')) { $microdata = $entity->microdata[$field['field_name']]; } foreach ($items as $delta => $item) { // Add the url attributes to $item['attributes'] because the theme function // will pass it through to l(), properly placing the itemprop for the url. if (isset($microdata['url'])) { $item['attributes'] += $microdata['url']['#attributes']; } // Pass the microdata array to the theme function so it can be used to place // the link title's attribute. $elements[$delta] = array( '#markup' => theme('link_formatter_'. $display['type'], array('element' => $item, 'field' => $instance, 'microdata' => $microdata)), ); } return $elements; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | /** * Theme function for 'default' text field formatter. */ function theme_link_formatter_link_default($vars) { $url = $vars['element']['url']; $microdata = $vars['microdata']; // If there is an itemprop set for the title, wrap the title in a span and // add the itemprop to that span. if (!empty($microdata['title'])) { $title = '<span ' . drupal_attributes($microdata['title']['#attributes']) . '>' . $vars['element']['title'] . '</span>'; } else { $title = $vars['element']['title']; } // Create the array of options to pass to l(). $link_options = $vars['element']; unset($link_options['element']['title']); unset($link_options['element']['url']); // Display a normal link if both title and URL are available. if (!empty($title) && !empty($url)) { return l($title, $url, $link_options); } // If only a title, display the title. elseif (!empty($title)) { return check_plain($title); } // If only a url, display the full url as a link. elseif (!empty($url)) { return l($url, $url, $link_options); } } |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |