6

I have a custom module. It's FormBase to create a form. And on submit, I post values to an API. All this works. But I want to change the base URL variable (which is used in multiple methods) so that when you on any environment besides live you get a different URL.

I want to figure out the Drupal 8 way of doing this.

class EventSuggestionForm extends FormBase {
  // Needs to be different if not the live site...
  private $baseUrl = 'https://www.example.com';
  private $apiUrl = '/api/v1/';
...

Can I get some advice?

10

You can create a new setting in your settings.local.php or settings.php file like:

$settings['event_base_url'] = 'https://www.example.com';

or some people are adding switch block based on the server host, e.g.:

switch (@$_SERVER['HTTP_HOST']) {
  default:
  case 'dev.example.com':
     $settings['event_base_url'] = 'http://dev.example.com';
     break;
  case 'stage.example.com':
     $settings['event_base_url'] = 'https://test.example.com';
     break;
  case 'www.example.com':
  case 'preprod.example.com':
     $settings['event_base_url'] = 'https://live.example.com';
     break;
}

Then reference it in the code like:

use Drupal\Core\Site\Settings;
$baseUrl = Settings::get('event_base_url', '');

So your code would look like:

use Drupal\Core\Site\Settings;
class EventSuggestionForm extends FormBase {
  private $baseUrl;
  private $apiUrl = '/api/v1/';

  function __construct() {
    // Read value from the settings file.
    $this->$baseUrl = Settings::get('event_base_url', '');
  }
...

Then change this setting per environment in the settings file, similar as you've different database credentials per different environment. This is the same concept as it's used for the existing $settings['file_public_base_url'] parameter.

  • 1
    Also see the State API which was built for similar use cases – Clive Aug 2 '17 at 22:59
  • 2
    Oh and you'll have to initialise $baseUrl in the constructor, default values for properties have to be constant – Clive Aug 2 '17 at 23:04
  • 1
    I actually looked into the State API a bit before posting. I read the part that stated: "So, use State API to store transient information, that is okay to lose after a reset." I was in a bit of a debate if this would qualify or not based on that. Honestly, I'm still a little unsure. – xpersonas Aug 3 '17 at 1:50
  • 1
    Note sure where you read about state and losing data, data stored in state is not lost, a lot of quite critical information for Drupal to work is stored in there. But settings and alternatively config overrides through settings.php is definitely much better suited for this. State API is mostly for things that code manages internally, for example Drupal uses it to store the last time cron was executed. Or whether the maintenance mode is enabled or not (there it makes sense because it is something that is temporarily enabled and then disabled again, not something you control per environment – Berdir Aug 3 '17 at 19:31
  • The comment before this was for @xpersonas. – kiamlaluno Aug 4 '17 at 12:00

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.