diff --git a/representative_image.module b/representative_image.module index 19f7476..2410c70 100644 --- a/representative_image.module +++ b/representative_image.module @@ -6,6 +6,13 @@ * which image field contains the appropriate image. */ +// The value of a representative image variable in case there is no +// representative image +define('REPRESENTATIVE_IMAGE_NONE', 0); +// The value of a representative image in case we want to fall back to the next +// sensible default. +define('REPRESENTATIVE_IMAGE_FALLBACK', 1); + /** * Implments hook_menu(). */ @@ -142,14 +149,49 @@ function representative_image($entity, $entity_type) { // how to set the default image, See the README.txt file. // @todo: Is there a better way to handle this? if (!$image) { - global $base_url; - $image = $base_url . '/' . variable_get('representative_image_default', ''); + $image = representative_image_default($entity_type, $bundle); } return $image; } /** + * Get the default representative image. + * + * @param $entity_type + * An entity type like "node", can also be NULL, in which case the bundle + * will be ignored and the site default will be returned. + * + * @param $bundle + * A bundle like "page" + */ +function representative_image_default($entity_type = NULL, $bundle = NULL) { + global $base_url; + // the global can any of the following + if ($entity_type && $bundle) { + $default = variable_get('representative_image_default_' . $entity_type . '_' . $bundle, REPRESENTATIVE_IMAGE_FALLBACK); + if ($default == REPRESENTATIVE_IMAGE_FALLBACK) { + return representative_image_default($entity_type); + } + } + elseif ($entity_type) { + $default = variable_get('representative_image_default_' . $entity_type, REPRESENTATIVE_IMAGE_FALLBACK); + if ($default == REPRESENTATIVE_IMAGE_FALLBACK) { + return representative_image_default(); + } + } + else { + $default = variable_get('representative_image_default', preg_replace('/^' . str_replace('/', '\/', $base_url) . '\//', '', theme_get_setting('logo'))); + } + + if ($default) { + $default = $base_url . '/' . $default; + } + + return $default; +} + +/** * Given a node type machine name, returns available image fields. */ function representative_image_available_fields($entity_type, $bundle) { @@ -173,8 +215,24 @@ function representative_image_available_fields($entity_type, $bundle) { * Given an entity type and bundle, get its representative image or NULL. */ function representative_image_get_field($entity_type, $bundle) { - $field = variable_get('representative_image_field_' . $entity_type . '_' . $bundle, ''); - return array_key_exists($field, representative_image_available_fields($entity_type, $bundle)) ? $field : NULL; + // start by figuring out a sensible default value in case our field has + // not yet been defined. + $default = REPRESENTATIVE_IMAGE_NONE; + $available = representative_image_available_fields($entity_type, $bundle); + if (count($available)) { + $keys = array_keys($available); + $default = array_shift($keys); + } + + $field = variable_get('representative_image_field_' . $entity_type . '_' . $bundle, $default); + + // It is possible that we have an unavailable field in here, one that is + // temporarily unavailable or deleted. + // In that case we'll return REPRESENTATIVE_IMAGE_NONE. It should be noted + // that if admin/config/media/representative_image is visited in such a + // case, then saved, the value of the variable will change to + // REPRESENTATIVE_IMAGE_NONE without any feedback + return array_key_exists($field, representative_image_available_fields($entity_type, $bundle)) ? $field : REPRESENTATIVE_IMAGE_NONE; } /** diff --git a/tests/representative_image.test b/tests/representative_image.test index 25f2c32..651e8c9 100644 --- a/tests/representative_image.test +++ b/tests/representative_image.test @@ -148,6 +148,20 @@ class RepresentativeImageEntitiesTestCase extends RepresentativeImageBaseTest { } /** + * Confirm that the defaults are sensible out of the box. + */ + function testDefaults() { + global $base_url; + $edit = array( + 'title' => ($article_image = $this->randomName()), + ); + $this->drupalPost('node/add/page', $edit, t('Save')); + $this->assertTrue(representative_image(node_load(1), 'node') == theme_get_setting('logo'), 'The global default image out of the box is the logo.'); + $this->assertTrue(representative_image_get_field('article', 'node') == 'field_image', 'The out-of-the-box default image field for a given entity is its first available image field if there is one.'); + $this->assertTrue(!representative_image_get_field('page', 'node'), 'The out-of-the-box default image field for a given entity is unset if no image field is available.'); + } + + /** * Confirm that node entities can have representative images. */ function testNodeTest() {