1

I have module config, something like:

modulename.config.yml:

modulename:
  core_address: 107.101.xx.yy
  core_port: 153759
  core_user: superuser
  core_pass: superpass

And I have module service, something like:

modulename.service.yml:

services:
    modulename.my_service:
        class: Drupal\modulename\group\MyService

MyService.php:

/**
 * @file
 * Contains \Drupal\modulename\group\MyService.
 */

namespace Drupal\modulename\group;

/**
 * MyService.
 */
class MyService{

  public function __construct() { }

  public function getDemoValue() {
    $config =  \Drupal::config('modulename.my_service');
    $core_port = $config->get('modulename.core_port');
    return $core_port. 'asdf';
  }

}

problem is:

that's $core_port return empty value...

and the question is:

how i can get access (read/write) my config from my module service?

not sure if this relevant, but getDemoValue() is called from different module as follow:

/**
 * @file
 * Contains \Drupal\anothermodulename\Controller\MyForm.
 */

namespace Drupal\anothermodulename\Controller;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Utility\UrlHelper;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * MyForm.
 */
class MyForm extends FormBase {

  protected $demoService;

  /**
   * Class constructor.
   */
  public function __construct($demoService) {
    $this->demoService = $demoService;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('modulename.my_service')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() { /** nothing interesting here **/ }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    /** nothing interesting here **/
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state){ /** nothing interesting here **/ }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    // Display result.
    foreach ($form_state->getValues() as $key => $value) {
      drupal_set_message($key . ': ' . $value . '  ' . $this->demoService->getDemoValue()); // i try to print here but didn't happend
    }
  }

}
  • Your config file seems strange to me. The schema name is 'module.config' and that is what you should use in calls to $config->get(). You probably do not need or want 'modulename' in the config schema file content. Look at language.negotiation.yml language.schema.yml and language_negotiation_url_prefixes_update() or a full example. – cilefen Jun 2 '16 at 17:55
  • @cilefen i dont have schema - only modulename.service.yml and modulename.config - do i need to have schema? – AnD Jun 4 '16 at 4:32
  • modulename.service.yml is not involved with this issue really. The problem is how you have named modulename.config.yml and the keys within it. A schema is necessary in order to have any tests and integration with the configuration system. – cilefen Jun 4 '16 at 13:30
  • $config = \Drupal::configFactory()->getEditable('modulename.config') loads the entire configuration, then ->get() retrieves individual values. Read over my answer and the suggestions and example I referenced. Also see a module I work on: cgit.drupalcode.org/mathjax/tree – cilefen Jun 4 '16 at 13:37
1

To read the variables (configs) you'd do this:

$config = \Drupal::config('modulename.my_service');

$core_port = $config->get('modulename.core_port');

To write to the variables (configs) you'd do this:

// Set and save new message value.

$config = \Drupal::service('config.factory')->getEditable('modulename.my_service');
$config->set('modulename.core_port', 153760)->save();

There's an example you can look at in the core module aggregator.

It has a settings form at /core/modules/aggregator/src/Form/SettingsForm.php

and then it adds a route to access the settings form:

aggregator.admin_settings:
  path: '/admin/config/services/aggregator/settings'
  defaults:
    _form: '\Drupal\aggregator\Form\SettingsForm'
    _title: 'Feed aggregator settings'
  requirements:
    _permission: 'administer news feeds'

You could pretty much copy the form into your module and change the variables to match your config.

Generally you'll want to add the config in your service constructor.

namespace Drupal\modulename;

/**
 * Example Service with config.
 */
class MyService {


  /**
   * Drupal's settings manager.
   */
  protected $settings;

  /**
   * Constructor.
   */
  public function __construct() {
    $this->settings = \Drupal::config('modulename.my_service');
  }

}

Then you'd access the variables like this:

$this->settings->get('modulename.core_port');
| improve this answer | |
  • I am not sure that config will be editable. – cilefen Jun 2 '16 at 15:38
  • $this->settings->get('modulename.core_port'); return nothing – AnD Jun 2 '16 at 16:11
  • did you try this? $config = \Drupal::config('modulename.my_service'); $core_port = $config->get('modulename.core_port'); – oknate Jun 2 '16 at 16:12
  • Also, keep in mind, you may need to reinstall your module with the config. You cannot add it after the fact. – oknate Jun 2 '16 at 16:12
  • yes just try reinstall the module and still return empty but no error – AnD Jun 2 '16 at 16:18
0

Use $config = \Drupal::configFactory()->getEditable('modulename.config').

And so on...

$core_port = $config->get('core_port');
$config->set('core_port', 8888)->save();

modulename.config.yml should be more like:

core_address: 107.101.xx.yy
core_port: 153759
core_user: superuser
core_pass: superpass
| improve this answer | |
  • do you mind to explain to me why i should put: arguments: ['@config.factory']? – AnD Jun 2 '16 at 15:40
  • and it throw me error: "Service::__construct() must implement interface Drupal\Core\Config\ConfigFactoryInterface, none given," – AnD Jun 2 '16 at 15:51
  • edited the answer... – cilefen Jun 2 '16 at 16:00
  • See also drupal.stackexchange.com/questions/164713/… – cilefen Jun 2 '16 at 16:05
  • i did return $config->get('modulename.core_port'); and it return nothing – AnD Jun 2 '16 at 16:10

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.