Is there a Drupal function to return all the multisite names?

I have searched a bit but have not come up with anything.

  • Are you talking about domain access? What is a multisite name for you? – Pere Jul 10 '14 at 19:39
  • I mean if I have sites/foo.com and sites/example.com, it would return array('foo.com', 'example.com'). – Elijah Lynn Jul 10 '14 at 19:41
  • ok, I'm not sure there's something built for this – Pere Jul 10 '14 at 19:49
  • each instance of drupal isn't necessarily aware of other instances. You could write a custom module that does a simple directory search under sites to return the values you want. – Geoff Jul 12 '14 at 16:47
  • 1
    There would (should?) not be an easy method in the Drupal API to do this, because multi-site instances are not supposed to be aware of each other. That said... I don't know what you're trying to achieve, but perhaps you could make it a requirement that the developer adds a sites.php file (see example.sites.php) and parse that file. – marcvangend Jul 14 '14 at 12:05

Okay, this is what I am using for now in a drushrc.php file. If you can improve upon this please post a new answer. Thanks

// Gets a list of all directories in /sites.
$sites = glob(drush_locate_root() . '/sites/*', GLOB_ONLYDIR);
// Strips the absolute path except for the last name.
$sites = array_map('basename', $sites);
// Removes the 'all' folder.
$sites = array_diff($sites, array('all'));
  • I like this solution but have replaced drush_locate_root() with DRUPAL_ROOT. It is set in index.php as the current working directory. – wranvaud Aug 22 '17 at 23:21

Another solution inspired by some team members and @marcvangend.

if (is_file(__DIR__ . '/../../sites.php')) {
  include __DIR__ . '/../../sites.php';
  $sites = array_keys($multisites);
  $sites[] = 'default';
  • Can I ask why do you need to do that? Why do you need the list of sites inside Drupal? – Pere Jul 14 '14 at 21:18
  • To make Drush look in a sites/{sitename}/drush/ folder for ../drush/commands, ../drush/aliases and ../drush/drushrc.php files. – Elijah Lynn Jul 15 '14 at 11:15
  • This supposes that the project makes use of the sites.php file for multisite aliasing. As a side note, the default variable name in sites.php is $sites. – wranvaud Aug 22 '17 at 23:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.