diff --git a/includes/calendar_plugin_row.inc b/includes/calendar_plugin_row.inc index f728d18..0e2a73b 100644 --- a/includes/calendar_plugin_row.inc +++ b/includes/calendar_plugin_row.inc @@ -52,7 +52,9 @@ class calendar_plugin_row extends views_plugin_row_fields { 'calendar_colors_vocabulary' => array('default' => array()), 'calendar_colors_taxonomy' => array('default' => array()), 'calendar_colors_group' => array('default' => array()), - )); + 'entityreference_field' => array('default' => ''), + 'calendar_colors_entityreference' => array('default' => array()), + )); return $options; } @@ -87,6 +89,10 @@ class calendar_plugin_row extends views_plugin_row_fields { if (module_exists('og')) { $options['group'] = t('Based on Organic Group'); } + if (module_exists('entityreference')) { + $options['entityreference'] = t('Based on Entity Reference'); + } + // If none of the options but the None option is available, stop here. if (count($options) == 1) { return; @@ -253,6 +259,77 @@ class calendar_plugin_row extends views_plugin_row_fields { ); } } + if (module_exists('entityreference')) { + $colors_entityreference = $this->options['colors']['calendar_colors_entityreference']; + + // Get the display's field names of entityreference fields. + $entityreference_field_options = array(); + $fields = $this->display->handler->get_option('fields'); + foreach ($fields as $name => $field_info) { + $field = field_info_field($name); + // Select the proper field type. + if (!empty($field['type']) && $field['type'] == 'entityreference') { + $entityreference_field_options[$name] = ($field_info['ui_name'] ? $field_info['ui_name'] : ($field_info['label'] ? $field_info['label'] : $field_info['id'])); + } + } + + $form['colors']['entityreference_field'] = array( + '#title' => t('Entity Reference field'), + '#type' => !empty($entityreference_field_options) ? 'select' : 'hidden', + '#default_value' => !empty($entityreference_field_options) ? $this->options['colors']['entityreference_field'] : '', + '#description' => t("Select the entity reference field to use when setting stripe colors. This works best for fields with only a limited number of referenceable entities."), + '#options' => $entityreference_field_options, + '#dependency' => array('edit-row-options-colors-legend' => array('entityreference')), + ); + + if (empty($entityreference_field_options)) { + $form['colors']['entityreference_field']['#options'] = array('' => ''); + $form['colors']['entityreference_field']['#suffix'] = t('You must add an entity reference field to this view to use entity reference stripe values. This works best for fields with only a limited number of referenceable entities.'); + } + + $options = array(); + if (isset($fields[$this->options['colors']['entityreference_field']])) { + $entityreference_field = field_info_field($this->options['colors']['entityreference_field']); + + $entity_type = $entityreference_field['settings']['target_type']; + foreach ($entityreference_field['settings']['handler_settings']['target_bundles'] as $bundle) { + $query = new EntityFieldQuery(); + $result = $query->entityCondition('entity_type', $entity_type) + ->entityCondition('bundle', $bundle) + ->execute(); + if (isset($result[$entity_type])) { + $entity_ids = array_keys($result[$entity_type]); + $entity_loaded = entity_load($entity_type, $entity_ids); + foreach ($entity_loaded as $entity_id => $entity_object) { + $options[$entity_id] = check_plain(t(entity_label($entity_type, $entity_object))); + } + } + } + + foreach ($options as $entity_id => $title) { + $form['colors']['calendar_colors_entityreference'][$entity_id] = array( + '#title' => check_plain($title), + '#default_value' => isset($colors_entityreference[$entity_id]) ? $colors_entityreference[$entity_id] : CALENDAR_EMPTY_STRIPE, + '#dependency' => array('edit-row-options-colors-legend' => array('entityreference')), + '#type' => 'textfield', + '#size' => 7, + '#maxlength' => 7, + '#element_validate' => array('calendar_validate_hex_color'), + '#prefix' => '
', + '#suffix' => '
', + '#attributes' => array('class' => array('edit-calendar-colorpicker')), + '#attached' => array( + // Add Farbtastic color picker. + 'library' => array( + array('system', 'farbtastic'), + ), + // Add javascript to trigger the colorpicker. + 'js' => array(drupal_get_path('module', 'calendar') . '/js/calendar_colorpicker.js'), + ), + ); + } + } + } } function options_submit(&$form, &$form_state) { @@ -502,6 +579,9 @@ class calendar_plugin_row extends views_plugin_row_fields { case 'group': $this->calendar_group_stripe($entity); break; + case 'entityreference': + $this->calendar_entityreference_stripe($entity); + break; } $rows[] = $entity; } @@ -686,4 +766,30 @@ class calendar_plugin_row extends views_plugin_row_fields { } return; } + + /** + * Create a stripe based on an entity reference field. + */ + function calendar_entityreference_stripe(&$result) { + $colors = isset($this->options['colors']['calendar_colors_entityreference']) ? $this->options['colors']['calendar_colors_entityreference'] : array(); + + if (empty($colors)) { + return; + } + + $field_name = $this->options['colors']['entityreference_field']; + $field = field_info_field($field_name); + + $entity_type = $field['settings']['target_type']; + + $property = 'field_' . $field_name; + foreach ($result->row->$property as $target_entity_wrapper) { + if (isset($colors[$target_entity_wrapper['raw']['target_id']])) { + $result->stripe[] = $colors[$target_entity_wrapper['raw']['target_id']]; + $target_entity = $target_entity_wrapper['raw']['entity']; + $result->stripe_label[] = check_plain(t(entity_label($entity_type, $target_entity))); + } + } + return; + } } diff --git a/theme/theme.inc b/theme/theme.inc index 5ae7b19..0f0bfcd 100644 --- a/theme/theme.inc +++ b/theme/theme.inc @@ -698,6 +698,28 @@ function theme_calendar_stripe_legend($vars) { } } break; + + case 'entityreference': + $fields = $view->display_handler->get_option('fields'); + if (isset($fields[$row_options['colors']['entityreference_field']])) { + $entityreference_field = field_info_field($row_options['colors']['entityreference_field']); + + $entity_type = $entityreference_field['settings']['target_type']; + foreach ($entityreference_field['settings']['handler_settings']['target_bundles'] as $bundle) { + $query = new EntityFieldQuery(); + $result = $query->entityCondition('entity_type', $entity_type) + ->entityCondition('bundle', $bundle) + ->execute(); + if (isset($result[$entity_type])) { + $entity_ids = array_keys($result[$entity_type]); + $entity_loaded = entity_load($entity_type, $entity_ids); + foreach ($entity_loaded as $entity_id => $entity_object) { + $options[$entity_id] = check_plain(t(entity_label($entity_type, $entity_object))); + } + } + } + } + break; } $header = array(