It would be nice if 'Custom links' in the XML sitemap could be on a domain basis.

An example of such, I'm working on a site that has a few custom dynamic pages in code and each domain may have different URLs based on certain domain based conditions, and as these pages aren't nodes or in a menu, I have opted to have them injected as 'Custom links' but there is no domain or access checking being done from that point onwards so all links are showing up in all domain sitemaps regardless to whether or not they should be.

To get around this I've just chosen to use a query alter, but it would be preferable to have the ability to set the links context.

Comments

ravencoder’s picture

Hi Deciphered,

I also trying to find a way to add 'custom links' based on domain.

Perhaps you can share your work around, to have this working?

deciphered’s picture

You're lucky I'm nice, while it probably would have been helpful had I shared the information straight away, finding the specifics from so long ago, especially when the project is long completed and the only copy I have is in a Time Machine backup of an old computer, was quite tricky... but find it I did.

This is only conceptual code, as I won't share a clients code without there permission, nor would some of the code even be of any use to you.

First thing I did was inside hook_cron() I would check if the custom links where already in the xmlsitemap table, as it was an ever growing list it was more than likely that there would be new items to build, in that case I would build up the link object and trigger it to be saved with code like the following:

$link = array(
  'type' => 'custom',
  'id' => db_query("SELECT MAX(id) FROM {xmlsitemap} WHERE type = 'custom'")->fetchField() + 1,
  'loc' => '[path to link]',
  'priority' => '0.5',
  'language' => 'und',
);
xmlsitemap_link_save($link);

Then comes the tricky bit, the query alter. It's tricky to really walk you through this one as it was done so long ago and I have no desire to get my head to deep in to it as I have no need to do so, so I'll just dump the slightly modified code and let you work it out for yourself:

function MYMODULE_query_xmlsitemap_generate_alter(QueryAlterableInterface $query) {
  $sitemap = $query->getMetaData('sitemap');
  if (!isset($sitemap->context['domain'])) {
    return;
  }

  // Showroom permissions check.
  $domain_condition = db_or();
  $array = explode(',', SOME_DOMAIN_SPECIFIC_CALLBACK());
  foreach ($array as $item) {
    $domain_condition->condition('loc', 'URL/' . $item);
  }
  $query->condition(
    db_or()->condition(
      db_and()->condition('loc', 'URL/%', 'LIKE')
        ->condition($domain_condition)
    )
    ->condition('loc', 'URL/%', 'NOT LIKE')
  );
}

Hope it helps

ravencoder’s picture

Hi Deciphered,

I really appreciate this. Thanks!

pdendis’s picture

Issue summary: View changes

I managed to create a sandbox module https://www.drupal.org/sandbox/pdendis/2344791. Below is the related code

/**
 * Implementation of hook_form_alter()
 *
 * Here we modify the add to cart form.
 */

function domain_xmlsitemap_customlinks_form_alter(&$form, &$form_state, $form_id ) {
	if (strstr($form_id, 'xmlsitemap_custom_edit_link_form')) {
		$link = $form_state['build_info']['args'][0];

		$domains = domain_domains();
		$options = array();

		$options['domain_0'] = t('All Domains');
		foreach ($domains as $domain) {
			$options["domain_".$domain['domain_id']] = $domain['path'];
		}
		$form['subtype'] = array(
			'#type' => 'select',
			'#title' => t('Domain'),
			'#options' => $options,
			'#default_value' => ((!empty($link['subtype'])) ? $link['subtype'] : 'domain_0')
		);
	}
}
/**
 * Implements hook_query_TAG_alter().
 */

function domain_xmlsitemap_customlinks_query_xmlsitemap_generate_alter(QueryAlterableInterface $query) {
  $sitemap = $query->getMetaData('sitemap');
  if (!isset($sitemap->context['domain'])) {
    return;
  }

  
  
  $custom_link_domain_condition = db_or();
  $custom_link_domain_condition->condition('type', 'custom', '!='); // exlude initially all custom
  $custom_link_domain_condition->condition(
  		db_and()->condition('type', 'custom', '=')
  				->condition(db_or()
  					->condition('subtype', 'domain_0', '=')
  					->condition('subtype', 'domain_'.$sitemap->context['domain'])
  				)
  );

  
  

  $query->condition($custom_link_domain_condition);
  
}
oleksiy’s picture

Version: 7.x-1.0-beta2 » 7.x-1.x-dev
Status: Active » Needs review
StatusFileSize
new2.43 KB

Added a patch based on the code above where it's allowed to choose several domains per custom link.