Skip to content

Commit

Permalink
Add aggregate rating, keywords, and video to recipe.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jen Lampton committed Jul 31, 2018
1 parent f149a2b commit f04e2f6
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
37 changes: 36 additions & 1 deletion structured_data.module
Expand Up @@ -188,9 +188,15 @@ function structured_data_form_node_type_form_alter(&$form, &$form_state) {
$image_source_options = array(
'' => t('- none -'),
);
$video_source_options = array(
'' => t('- none -'),
);
$duration_source_options = array(
'' => t('- none -'),
);
$rating_source_options = array(
'' => t('- none -'),
);

// Add fields from this node type to available data sources.
foreach (field_info_instances('node', $type->type) as $field_name => $value) {
Expand All @@ -204,6 +210,9 @@ function structured_data_form_node_type_form_alter(&$form, &$form_state) {
elseif ($field_info['type'] == 'image') {
$image_source_options[$field_info['field_name']] = $field_instance['label'];
}
elseif ($field_info['type'] == 'youtube') {
$video_source_options[$field_info['field_name']] = $field_instance['label'];
}
elseif ($field_info['module'] == 'number') {
$duration_source_options[$field_info['field_name']] = $field_instance['label'];
}
Expand All @@ -213,6 +222,9 @@ function structured_data_form_node_type_form_alter(&$form, &$form_state) {
elseif ($field_info['module'] == 'reference') {
$text_source_options[$field_info['field_name']] = $field_instance['label'];
}
elseif ($field_info['module'] == 'fivestar') {
$rating_source_options[$field_info['field_name']] = $field_instance['label'];
}
}

// Map the field data to structured data.
Expand All @@ -226,6 +238,9 @@ function structured_data_form_node_type_form_alter(&$form, &$form_state) {
case 'ImageObject':
$source_options += $image_source_options;
break;
case 'VideoObject':
$source_options += $video_source_options;
break;
case 'DateTime':
$source_options += $date_source_options;
break;
Expand All @@ -238,6 +253,9 @@ function structured_data_form_node_type_form_alter(&$form, &$form_state) {
case 'Duration':
$source_options += $duration_source_options;
break;
case 'AggregateRating':
$source_options += $rating_source_options;
break;
}
}

Expand Down Expand Up @@ -448,6 +466,7 @@ function structured_data_get_node_info($node) {
// Get the field type.
$field_info = field_info_field($field);

// @todo make these into hooks so any module can implement?
switch ($field_info['module']) {
case 'image':
if (array_key_exists('uri', $node->{$field}[$node->langcode][0])) {
Expand All @@ -462,6 +481,15 @@ function structured_data_get_node_info($node) {
}
break;

case 'youtube':
if (array_key_exists('input', $node->{$field}[$node->langcode][0])) {
$data[$property] = array(
'@type' => 'VideoObject',
'embedUrl' => $node->{$field}[$node->langcode][0]['input'],
);
}
break;

case 'number':
if (array_key_exists('value', $node->{$field}[$node->langcode][0])) {
$schema_type = reset($property_info['type']);
Expand All @@ -473,6 +501,13 @@ function structured_data_get_node_info($node) {
}
break;

case 'fivestar':
if (array_key_exists('average', $node->{$field}[$node->langcode][0])) {
// Divide b/c votingAPI works in percents, but we're using 5 stars.
$data[$property] = $node->{$field}[$node->langcode][0]['average']/20;
}
break;

default: // text module
if (array_key_exists('value', $node->{$field}[$node->langcode][0])) {
$data[$property] = filter_xss(strip_tags($node->{$field}[$node->langcode][0]['value']));
Expand All @@ -491,7 +526,7 @@ function structured_data_get_node_info($node) {
} // foreach $property_info
} // if enabled node type

dpm($data);
//dpm($data);

return $data;
}
Expand Down
21 changes: 18 additions & 3 deletions structured_data.schemas.inc
Expand Up @@ -172,6 +172,11 @@ function structured_data_get_schemas($type = NULL) {
'description' => 'Images should be at least 696 pixels wide.',
'type' => array('ImageObject'),
),
'video' => array(
'name' => 'Video',
'description' => 'An embedded video object or a video file.',
'type' => array('VideoObject'),
),
'description' => array(
'name' => 'Description',
'description' => 'A description of the recipe.',
Expand Down Expand Up @@ -202,9 +207,9 @@ function structured_data_get_schemas($type = NULL) {
'description' => 'The total time required to perform instructions or a direction (including time to prepare the supplies), in ISO 8601 duration format.',
'type' => array('Duration'),
),
'cookingMethod' => array(
'name' => 'Cooking Method',
'description' => 'The method of cooking, such as Frying, Steaming, ...',
'keywords' => array(
'name' => 'Keywords',
'description' => 'Keywords or tags used to describe this content. Multiple entries in a keywords list are typically delimited by commas.',
'type' => array('Text'),
),
'recipeCategory' => array(
Expand All @@ -217,6 +222,11 @@ function structured_data_get_schemas($type = NULL) {
'description' => 'The cuisine of the recipe (for example, French or Ethiopian).',
'type' => array('Text'),
),
'cookingMethod' => array(
'name' => 'Cooking Method',
'description' => 'The method of cooking, such as Frying, Steaming, ...',
'type' => array('Text'),
),
'recipeIngredient' => array(
'name' => 'Recipe Ingredient',
'description' => 'A single ingredient used in the recipe, e.g. sugar, flour or garlic.',
Expand All @@ -227,6 +237,11 @@ function structured_data_get_schemas($type = NULL) {
'description' => 'A step in making the recipe, in the form of a single item (document, video, etc.) or an ordered list with HowToStep and/or HowToSection items.',
'type' => array('Text', 'ItemList', 'CreativeWork'),
),
'aggregateRating' => array(
'name' => 'AggregateRating ',
'description' => 'The overall rating, based on a collection of reviews or ratings, of the item.',
'type' => array('AggregateRating'),
),
'recipeYield' => array(
'name' => 'Recipe Yield',
'description' => 'The quantity produced by the recipe (for example, number of people served, number of servings, etc).',
Expand Down

0 comments on commit f04e2f6

Please sign in to comment.