diff --git a/xmlsitemap_entity/xmlsitemap_entity.module b/xmlsitemap_entity/xmlsitemap_entity.module index aaba299..54d5f21 100644 --- a/xmlsitemap_entity/xmlsitemap_entity.module +++ b/xmlsitemap_entity/xmlsitemap_entity.module @@ -23,58 +23,73 @@ function xmlsitemap_entity_cron() { */ function xmlsitemap_entity_entity_info_alter(array &$entity_info) { foreach ($entity_info as &$info) { - // Skip entities that shouldn't be handled by this module. - if (isset($info['xmlsitemap']) && $info['xmlsitemap'] == FALSE) { - unset($info['xmlsitemap']); + // Skip this entity type if we already have all the info we need. + if (isset($info['xmlsitemap']['process callback'])) { continue; } - elseif (isset($info['xmlsitemap']['process callback'])) { + + // Skip entities that shouldn't be handled by this module or that simply + // can't be indexed. + $indexable = xmlsitemap_entity_entity_type_is_indexable(); + + if (!$indexable) { + unset($info['xmlsitemap']); continue; } - // Check if this type of entity can be indexed. - $indexable = (isset($info['entity keys']['id']) && isset($info['uri callback']) && isset($info['access callback'])); - - if ($indexable) { - // The default Entity API uri callback is ignored unless it has been - // overridden in the child class, this is checked using reflection. - if ($info['uri callback'] == 'entity_class_uri') { - $indexable = FALSE; + if (!isset($info['xmlsitemap']) || !is_array($info['xmlsitemap'])) { + $info['xmlsitemap'] = array(); + } - if (isset($info['entity class']) && $info['entity class'] != 'Entity') { - $class = new ReflectionClass($info['entity class']); - $default_uri = $class->getMethod('defaultUri')->getDeclaringClass()->getName(); - $uri = $class->getMethod('uri')->getDeclaringClass()->getName(); + // Set a bundle label. + $info += array( + 'bundle label' => (isset($info['entity keys']['bundle']) ? t('Bundle') : $info['label']), + ); + + // Add the XML sitemap info. + $info['xmlsitemap'] += array( + 'use entity module' => TRUE, + 'changed property' => NULL, + 'process callback' => 'xmlsitemap_entity_xmlsitemap_process_entity_links', + ); + } +} - if (in_array($info['entity class'], array($default_uri, $uri))) { - $indexable = TRUE; - } - } - } +/** + * Checks if an entity type is indexable by its entity_info. + * + * @param array $entity_info + * The entity info array of the entity type to check. + * + * @return boolean + * True if indexable by xmlsitemap, false otherwise. + */ +function xmlsitemap_entity_entity_type_is_indexable($entity_info) { + $indexable = isset($info['xmlsitemap']) + && $info['xmlsitemap'] !== FALSE + && isset($info['entity keys']['id']) + && isset($info['uri callback']) + && isset($info['access callback']); + + if (!$indexable) { + return FALSE; + } - if ($indexable) { - if (!isset($info['xmlsitemap']) || !is_array($info['xmlsitemap'])) { - $info['xmlsitemap'] = array(); - } + // The default Entity API uri callback is ignored unless it has been + // overridden in the child class, this is checked using reflection. + if ($info['uri callback'] == 'entity_class_uri') { + if (isset($info['entity class']) && $info['entity class'] != 'Entity') { + $class = new ReflectionClass($info['entity class']); + $default_uri = $class->getMethod('defaultUri')->getDeclaringClass()->getName(); + $uri = $class->getMethod('uri')->getDeclaringClass()->getName(); - // Set a bundle label. - $info += array( - 'bundle label' => (isset($info['entity keys']['bundle']) ? t('Bundle') : $info['label']), - ); - - // Add the XML sitemap info. - $info['xmlsitemap'] += array( - 'use entity module' => TRUE, - 'changed property' => NULL, - 'process callback' => 'xmlsitemap_entity_xmlsitemap_process_entity_links', - ); + if (in_array($info['entity class'], array($default_uri, $uri))) { + return TRUE; } } - - if (!$indexable && isset($info['xmlsitemap'])) { - unset($info['xmlsitemap']); - } + return FALSE; } + return TRUE; } /** @@ -99,23 +114,25 @@ function xmlsitemap_entity_xmlsitemap_link_info_alter(&$link_info) { * Implements hook_form_alter(). */ function xmlsitemap_entity_form_alter(&$form, &$form_state, $form_id) { - if (isset($form_state['entity_type']) && isset($form_state[$form_state['entity_type']])) { - if (isset($form_state['op']) && in_array($form_state['op'], array('add', 'create', 'edit', 'update'))) { - $info = entity_get_info($form_state['entity_type']); + if (!isset($form_state['entity_type']) || !isset($form_state[$form_state['entity_type']])) { + return; + } + if (!isset($form_state['op']) || !in_array($form_state['op'], array('add', 'create', 'edit', 'update'))) { + return; + } + $info = entity_get_info($form_state['entity_type']); - if (!empty($info['bundle of'])) { - $entity_type = $form_state[$form_state['entity_type']]->{$info['entity keys']['name']}; + if (!empty($info['bundle of'])) { + $entity_type = $form_state[$form_state['entity_type']]->{$info['entity keys']['name']}; - form_load_include($form_state, 'inc', 'xmlsitemap', 'xmlsitemap.admin'); - xmlsitemap_add_link_bundle_settings($form, $form_state, $info['bundle of'], $entity_type); - } - elseif (!empty($info['xmlsitemap']['use entity module'])) { - list($entity_id, , $bundle) = entity_extract_ids($form_state['entity_type'], $form_state[$form_state['entity_type']]); + form_load_include($form_state, 'inc', 'xmlsitemap', 'xmlsitemap.admin'); + xmlsitemap_add_link_bundle_settings($form, $form_state, $info['bundle of'], $entity_type); + } + elseif (!empty($info['xmlsitemap']['use entity module'])) { + list($entity_id, , $bundle) = entity_extract_ids($form_state['entity_type'], $form_state[$form_state['entity_type']]); - form_load_include($form_state, 'inc', 'xmlsitemap', 'xmlsitemap.admin'); - xmlsitemap_add_form_link_options($form, $form_state['entity_type'], $bundle, $entity_id); - } - } + form_load_include($form_state, 'inc', 'xmlsitemap', 'xmlsitemap.admin'); + xmlsitemap_add_form_link_options($form, $form_state['entity_type'], $bundle, $entity_id); } } @@ -181,29 +198,31 @@ function xmlsitemap_entity_link_bundle_access($entity_data) { */ function xmlsitemap_entity_xmlsitemap_index_links($limit) { foreach (entity_get_info() as $entity_type => $info) { - if (!empty($info['xmlsitemap']['use entity module'])) { - $bundles = xmlsitemap_get_link_type_enabled_bundles($entity_type); - - if ($bundles) { - $query = db_select($info['base table'], 'e') - ->fields('e', array($info['entity keys']['id'])) - ->isNull('s.id') - ->orderBy('e.' . $info['entity keys']['id'], 'DESC') - ->range(0, $limit); - $query->leftJoin('xmlsitemap', 's', 'e.' . $info['entity keys']['id'] . ' = s.id AND s.type = :type', array( - ':type' => $entity_type, - )); - - if (!empty($info['entity keys']['bundle'])) { - $query->condition('e.' . $info['entity keys']['bundle'], $bundles); - } + if (empty($info['xmlsitemap']['use entity module'])) { + continue; + } + $bundles = xmlsitemap_get_link_type_enabled_bundles($entity_type); + + if (!$bundles) { + continue; + } + $query = db_select($info['base table'], 'e') + ->fields('e', array($info['entity keys']['id'])) + ->isNull('s.id') + ->orderBy('e.' . $info['entity keys']['id'], 'DESC') + ->range(0, $limit); + $query->leftJoin('xmlsitemap', 's', 'e.' . $info['entity keys']['id'] . ' = s.id AND s.type = :type', array( + ':type' => $entity_type, + )); + + if (!empty($info['entity keys']['bundle'])) { + $query->condition('e.' . $info['entity keys']['bundle'], $bundles); + } - $ids = $query->execute()->fetchCol(); + $ids = $query->execute()->fetchCol(); - if ($ids) { - xmlsitemap_entity_xmlsitemap_process_entity_links($ids, array('entity_type' => $entity_type)); - } - } + if ($ids) { + xmlsitemap_entity_xmlsitemap_process_entity_links($ids, array('entity_type' => $entity_type)); } } } diff --git a/xmlsitemap_file/xmlsitemap_file.module b/xmlsitemap_file/xmlsitemap_file.module index 8814216..ff6a96e 100644 --- a/xmlsitemap_file/xmlsitemap_file.module +++ b/xmlsitemap_file/xmlsitemap_file.module @@ -1,7 +1,7 @@ 'checkbox', @@ -23,25 +23,26 @@ function xmlsitemap_file_form_file_entity_file_type_form_alter(&$form, &$form_st } /** - * Implements hook_xmlsitemap_element_alter + * Implements hook_xmlsitemap_element_alter(). */ -function xmlsitemap_file_xmlsitemap_element_alter(array &$element, array $link, $sitemap) { - // See definition of hook_xmlsitemap_element_alter in xmlsitemap.api.php for +function xmlsitemap_file_xmlsitemap_element_alter(array &$element, array $link, $sitemap) { + // See definition of hook_xmlsitemap_element_alter in xmlsitemap.api.php for // sample code to add additonal element info or metadata. - if ($link['type'] == 'file') { - // Verify that this file subtype needs /download appended to it's path - $bundle = $link['subtype']; - $config = variable_get('xmlsitemap_settings_file_' . $bundle , FALSE); - $download = isset($config['download']) ? $config['download'] : FALSE ; - - if ($download) { - $element['loc'] .= '/download'; - } + if ($link['type'] !== 'file') { + return; + } + // Verify that this file subtype needs /download appended to it's path + $bundle = $link['subtype']; + $config = variable_get('xmlsitemap_settings_file_' . $bundle , FALSE); + $download = isset($config['download']) ? $config['download'] : FALSE ; + + if ($download) { + $element['loc'] .= '/download'; } } /** - * Implements hook_entity_info_alter + * Implements hook_entity_info_alter(). */ function xmlsitemap_file_entity_info_alter(&$entity_info) { // Set $entity->timestamp as last modification date of file @@ -61,11 +62,12 @@ function xmlsitemap_file_form_file_entity_edit_alter(array &$form, array &$form_ // combination of modules. But even if those modules are not being used, this should // still behave the same, since the callback will still fire by virtue of being // attached to the submit button element. - if (!empty($form['actions']['submit'])) { - $form['actions']['submit'] += array('#submit' => array()); - array_unshift($form['actions']['submit']['#submit'], 'xmlsitemap_process_form_link_options'); - if (($key = array_search('xmlsitemap_process_form_link_options', $form['#submit'])) !== FALSE) { - unset($form['#submit'][$key]); - } + if (empty($form['actions']['submit'])) { + return; } -} \ No newline at end of file + $form['actions']['submit'] += array('#submit' => array()); + array_unshift($form['actions']['submit']['#submit'], 'xmlsitemap_process_form_link_options'); + if (($key = array_search('xmlsitemap_process_form_link_options', $form['#submit'])) !== FALSE) { + unset($form['#submit'][$key]); + } +}