结合使用 Drupal、HTML5 和微数据(3)
 
- UID
- 1066743
|

结合使用 Drupal、HTML5 和微数据(3)
在字段格式器中启用微数据文本与图像字段涵盖了人们通常会在站点上放置的大量数据,但仍然存在其他类型的数据。为了涵盖站点管理员可能需要的所有类型的数据,Drupal 的字段系统允许用户选择基本的字段类型,并提供了一个 API,让模块能够定义新的字段类型。在这些模块中,您可以为每种字段类型定义不同的数据收集表单(小部件)、数据存储与显示(格式器)。然后站点管理员就可以安装这些字段模块,并在不必编写任何代码的情况下配置小部件与格式器 (formatter)。
对于在 HTML 中放置微数据属性的位置,微数据有严格的要求,因此 Drupal 中的每种字段类型都需要定义在其格式器中放置属性的位置。微数据得到了核心定义的大多数字段类型的支持,但很多广泛应用的字段类型仍然不支持微数据。
要使用所贡献模块中定义的字段格式器,您可以查看追踪微数据支持的表格。即使尚未受到支持的字段格式器,这并不意味着您不能使用它。给字段格式器添加微数据支持十分简单。您甚至可以通过创建一个包含您所修改内容的补丁,将微数据支持传回给该模块。这是加入 Drupal 开发人员社区的一种好方法。
在来自 schema.org 的例子中,标记了一个指向电影预告片的链接。在撰写本文之际,Drupal Link 模块定义的链接字段格式器还不支持微数据,但您可以改变这一点。
您将为 Link 模块添加微数据支持。下面的例子使用了 2011 年 9 月 20 日的 Link 模块代码,本文的 文件中提供了这些代码。(Link 模块的当前版本已发生改变,可能已经包含对微数据的支持。)
注册属性链接字段有两个不同的数据位 (bit of data),您可能想使用微数据公开它们:
此时,您需要通过一个 Entity API 模块(即 Entity Property API)向系统通知这两个属性。
您必须向字段定义中添加一些信息,这些信息是通过 link_field_info 注册的。添加字段本身的 property_type 和 property_callbacks,如 中所示。
清单 2. 将字段的属性信息添加到 link_field_info1
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'),
),
);
}
|
属性类型可以让系统知道字段的数据类型。因为 field_item_link 不是可以识别的数据类型或实体,在处理它们时,数据类型默认为 struct。这个 struct 就好比是已标记属性(链接 URL 与被链接的文本)的一个容器。因为它只是一个容器而已,所以您不必为字段本身启用微数据,只需为它的属性启用微数据即可。
属性回调是为组件属性注册相同属性类型信息的一项功能。要使用微数据标记属性,需要针对每个属性将微数据设置为 TRUE,如 中所示。这为针对这些属性添加微数据提供了图形化用户界面。
清单 3. 使用属性回调注册字段的属性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']);
}
}
|
用户界面从属性信息中获取标签,并使用类型来确定要显示哪种类型的表单字段。如果属性是一个内容项而非字符串,那么该界面上还会显示一个 itemtype 字段。 显示了一部预告片的两个属性的示例:链接标题与链接 URL。
图 7. 链接微数据映射表单 您现在可以在字段配置表单上指定对字段属性使用哪些词汇表术语。然而,直到添加一点代码之后,属性才会插入到 HTML 中。
给有主题输出添加微数据为了放置微数据,您需要修改字段的 HTML 输出。例如,为了向软件应用程序添加一个链接,您可能想让链接文本(软件名称)使用 name 属性,而让链接本身使用 url 属性。 显示了如何做到这一点,它向标签添加 URL 的 itemprop,并使用文本的 itemprop 在文本内容周围插入空白。
清单 4. 添加微数据之前与之后的一个链接1
2
3
| <a href="http://drupal.org">Drupal</a>
<a itemprop="url" href="http://drupal.org"><span itemprop="name">Drupal</span></a>
|
如果您能够使用 Link 模块插入这些属性,操作就会变得更简单。为了将来自数据库的字段内容转换为 HTML,每个字段格式器模块都有自己的查看函数。在查看函数中,一些格式器使用主题函数来生成 HTML。theme_link_formatter_link_default() 就是这样的一个例子。通常,需要将微数据属性从 field_formatter_view 函数解析到主题函数中。
在 Link 模块中,该格式器已经使用 item 变量传递了一个要放置在 <a> 标签上的属性数组。您可以将 URL itemprop 添加到该数组中,在需要时自动输出它,如 中所示。
清单 5. 在 hook_field_formatter_view 中添加微数据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;
}
|
无法自动放置文本内容的属性。您必须将它们传递到主题函数中,并修改主题函数,然后才能使用它们。
将微数据变量传递给主题函数之后,您可以在主题周围添加包含 itemprop 的 <span> 标签。然后进行代码检查,以确定是否存在用于文本的 itemprop,如果存在,则添加微数据,如 清单 6 中所示。
清单 6. 在主题函数中添加微数据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);
}
}
|
现在,您可以为格式器测试微数据输出。
将您的修改反馈给社区Drupal 能成为功能强大的技术解决方案的原因之一是:它拥有数量庞大的社区贡献者。这些贡献者们不仅是与 Drupal 同呼吸共命运的人,还会偶尔对自己的站点进行代码修复,然后将这些修复打包为补丁,供其他人使用。
如果您在自己的项目中给字段格式器添加微数据,您可以将这些成果反馈给 Drupal 社区。只需在模块的文章队列中发表一篇文章,并建议该模块支持微数据即可。这类文章统称为功能建议。然后,您可以在文章中发布一个包含您所做修改的补丁。(一些优秀的指南还演示了如何为 Drupal 项目创建补丁。)补丁发布成功后,就会标记为 “需要审查”。 |
|
|
|
|
|