2

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

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

7
  • Are you talking about domain access? What is a multisite name for you?
    – Pere
    Commented Jul 10, 2014 at 19:39
  • I mean if I have sites/foo.com and sites/example.com, it would return array('foo.com', 'example.com'). Commented Jul 10, 2014 at 19:41
  • ok, I'm not sure there's something built for this
    – Pere
    Commented Jul 10, 2014 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
    Commented Jul 12, 2014 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. Commented Jul 14, 2014 at 12:05

2 Answers 2

1

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'));
1
  • 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
    Commented Aug 22, 2017 at 23:21
0

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';
}
3
  • Can I ask why do you need to do that? Why do you need the list of sites inside Drupal?
    – Pere
    Commented Jul 14, 2014 at 21:18
  • To make Drush look in a sites/{sitename}/drush/ folder for ../drush/commands, ../drush/aliases and ../drush/drushrc.php files. Commented Jul 15, 2014 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
    Commented Aug 22, 2017 at 23:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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