1

In the settings.php file, there are (for example) $base_url, $settings['hash_salt'].

I defined $settings['external_url'] = "google.com";

In my custom module, I want to get the $settings['external_url'] value.

<?php

namespace Drupal\my_custom_module\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Site\Settings;
use Drupal\Core\Form\FormStateInterface;

/**
* Contribute form.
*/
class MyCustomClass extends FormBase {

   private $base_url;
   public function __construct(){
       $this->base_url = Settings::get('external_url') . '/maps';

   }

   public function validateForm(array &$form, FormStateInterface $form_state) {
      $pb= $form_state->getValue('pb');
      $url = $this->base_url . '?timeline' . $pb;
   }
}

Then I got this error:

Notice: Trying to get property of non-object

I am using Drupal 8 and PHP 7.

1 Answer 1

1

Trying using the full path of the class:

\Drupal\Core\Site\Settings::get('external_url')

In Drupal 8, an ideal place for custom variables is in config files. Create a yml file in the config directory and import the configuration.

my_variables.settings.yml

external_url: 'url-string-here'

Then to fetch the values, it's:

\Drupal::config('my_variables.settings')->get('external_url')

https://www.drupal.org/docs/8/converting-drupal-7-modules-to-drupal-8/step-4-convert-drupal-7-variables-to-drupal-8

0

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.