diff --git a/birthdays.info b/birthdays.info index 7578015..c636b9e 100644 --- a/birthdays.info +++ b/birthdays.info @@ -3,6 +3,7 @@ description = Provides a birthday field type. Displays and reminds of upcoming b dependencies[] = field files[] = birthdays.test files[] = birthdays_birthday.inc +files[] = birthdays.microdata.inc files[] = views/birthdays_field_views_handler_field.inc files[] = views/birthdays_field_views_handler_sort.inc files[] = views/birthdays_field_views_handler_filter.inc diff --git a/birthdays.microdata.inc b/birthdays.microdata.inc new file mode 100644 index 0000000..bbea4e4 --- /dev/null +++ b/birthdays.microdata.inc @@ -0,0 +1,24 @@ + array('person'), + '#is_item' => TRUE, + '#itemtype' => array('http://schema.org/Person'), + 'birthday' => array( + '#itemprop' => array('birthDate'), + ), + ); + + return $suggestions; +} diff --git a/birthdays.module b/birthdays.module index d81e1ea..6e85d5e 100644 --- a/birthdays.module +++ b/birthdays.module @@ -298,7 +298,7 @@ function theme_birthdays_date($variables) { unset($element['triggers']); } - return '' . drupal_render_children($element) . '' . drupal_render($triggers); + return '' . drupal_render_children($element) . '' . drupal_render($triggers); } /** @@ -320,6 +320,9 @@ function birthdays_field_info() { 'description' => '', ), ), + // Integrate with the Entity Metadata module. + 'property_type' => 'date', + 'property_callbacks' => array('birthdays_entity_metadata_property_info_alter'), ), ); } @@ -455,6 +458,7 @@ function birthdays_field_formatter_settings_summary($field, $instance, $view_mod */ function birthdays_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) { $element = array(); + $microdata = array(); foreach ($items as $delta => $item) { $birthday = BirthdaysBirthday::fromArray($item); @@ -465,36 +469,71 @@ function birthdays_field_formatter_view($entity_type, $entity, $field, $instance switch ($display['type']) { case 'birthdays_plaintext': - $element[$delta] = array('#markup' => check_plain($birthday->toString($display['settings']['dateformat'], $display['settings']['dateformat_noyear']))); + $element[$delta] = array( + 'children' => array( + 'visible_output' => array( + '#markup' => check_plain($birthday->toString($display['settings']['dateformat'], $display['settings']['dateformat_noyear'])) + ), + ), + ); break; case 'birthdays_starsign': if (!$birthday->isEmpty()) { $element[$delta] = array( - '#theme' => 'image', - '#path' => url(drupal_get_path('module', 'birthdays') . '/starsigns/' . $birthday->getStarsign() . '.gif'), - '#height' => '35', - '#width' => '35', - '#alt' => t(ucfirst($birthday->getStarsign())), - '#title' => t(ucfirst($birthday->getStarsign())), + 'children' => array( + 'visible_output' => array( + '#theme' => 'image', + '#path' => url(drupal_get_path('module', 'birthdays') . '/starsigns/' . $birthday->getStarsign() . '.gif'), + '#height' => '35', + '#width' => '35', + '#alt' => t(ucfirst($birthday->getStarsign())), + '#title' => t(ucfirst($birthday->getStarsign())), + ), + ), ); } break; case 'birthdays_age': if ($birthday->getYear()) { - $element[$delta] = array('#markup' => check_plain($birthday->getCurrentAge())); + $element[$delta] = array( + 'children' => array( + 'visible_output' => array( + '#markup' => check_plain($birthday->getCurrentAge()) + ), + ), + ); } break; case 'birthdays_age_upcoming': if ($birthday->getYear()) { - $element[$delta] = array('#markup' => check_plain($birthday->getCurrentAge() + 1)); + $element[$delta] = array( + 'children' => array( + 'visible_output' => array( + '#markup' => check_plain($birthday->getCurrentAge() + 1) + ), + ), + ); } break; } } + // 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']]; + + $timezone = new DateTimeZone('Etc/Zulu'); + $date_object = new DateTime("{$birthday->getYear()}/{$birthday->getMonth()}/{$birthday->getDay()}", $timezone); + $microdata['birthday']['#attributes']['content'] = $date_object->format("Y-m-d\TH:i:s"); + $element[$delta]['children']['microdata_output'] = array( + '#markup' => '', + ); + } + return $element; } @@ -1125,3 +1164,67 @@ function birthdays_mail($key, &$message, $params) { break; } } + +/** + * Additional callback to adapt the property info of birthdays fields. + + * @see entity_metadata_field_entity_property_info(). + */ + +function birthdays_entity_metadata_property_info_alter(&$info, $entity_type, $field, $instance, $field_type) { + $name = $field['field_name']; + $property = &$info[$entity_type]['bundles'][$instance['bundle']]['properties'][$name]; + + + $property['property info'] = array( + 'birthday' => array( + 'type' => 'date', + 'label' => t('Birthday'), + 'getter callback' => 'birthdays_entity_metadata_struct_getter', + 'setter callback' => 'birthdays_entity_metadata_struct_setter', + // The getter and setter callbacks for 'birthdays' will not provide the + // field name as $name, we'll add it to $info. + 'field_name' => $field['field_name'], + // Alert Microdata module that this value can be exposed in microdata. + 'microdata' => TRUE, + ), + ); +} + +/** + * Getter callback to return date values as datestamp in UTC. + */ +function birthdays_entity_metadata_struct_getter($item, array $options, $name, $type, $info) { + $value = trim($item[$name]); + if (empty($value)) { + return NULL; + } + + if (birthdays_field_is_empty($item, field_info_field($info['field_name']))) { + + $timezone = new DateTimeZone('Etc/Zulu'); + $birthday = new DateTime($value, $timezone); + + return !empty($birthday) ? $birthday->format("Y-m-d\TH:i:s") : NULL; + } + return NULL; +} + +/** + * Callback for setting an individual date value. + * Based on entity_property_verbatim_set(). + * + * The passed in unix timestamp (UTC) is converted to the right value and + * format dependent on the field. + */ +function birthdays_entity_metadata_struct_setter(&$item, $name, $value, $langcode, $type, $info) { + if (!isset($value)) { + $item[$name] = NULL; + } + else { + $timezone = new DateTimeZone('Etc/Zulu'); + $birthday = new DateTime($value, $timezone); + + $item[$name] = $birthday->format("Y-m-d\TH:i:s"); + } +}