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' => '