1

I have made a simple example to practice using the configuration API in Drupal 8. I created the twitter_pull.credentials.yml file in the config/install folder with the following content and flushed the cache.

oauth_access_token: "12345"
oauth_access_token_secret: "67890"
consumer_key: "54321"
consumer_secret: "09876"

In a custom block, I use the following code to retrieve the configuration object.

namespace Drupal\twitter_pull\Plugin\Block;
use Drupal\block\BlockBase;

/**
 * Provides a block for executing PHP code.
 *
 * @Block(
 *   id = "twitter_pull_tweets_block",
 *   admin_label = @Translation("Twitter Tweets")
 * )
 */
class TweetsBlock extends BlockBase {

  /**
   * Builds and returns the renderable array for this block plugin.
   *
   * @return array
   *   A renderable array representing the content of the block.
   *
   * @see \Drupal\block\BlockViewBuilder
   */
  public function build() {
    $config = \Drupal::config('twitter_pull.credentials');
    $enabled = \Drupal::config('system.maintenance')->get('enabled');
    dpm($enabled);
    dpm($config);
    drupal_set_message($enabled);
    return 'this is a block: ' . $config->get('consumer_key');
  }
}

I don't get any value from \Drupal::config('system.maintenance')->get('enabled') nor $config->get('consumer_key').

What am I missing here?

6
  • You haven't mentioned a config schema so read this first: drupal.org/node/1905070
    – Clive
    Aug 18, 2014 at 12:50
  • Configuration schema is option in core (at least for now). So configuration should work without schema. It looks like either cache or scope where you are trying to retrieve. can you provide some more code or steps that you did you to enable module/block print etc
    – vijaycs85
    Aug 18, 2014 at 14:27
  • 1
    I would assume you installed the module before creating the config file, so the config was never imported. Aug 18, 2014 at 15:21
  • @vijaycs85 - I have added the extra code. Aug 18, 2014 at 15:21
  • 1
    @DanielWehner has a good point. See if uninstall/install module solves the problem. Also only elements available in system.maintenance are: message and langcode. try to check $message = \Drupal::config('system.maintenance')->get('message'); dpm($message);
    – vijaycs85
    Aug 18, 2014 at 15:34

1 Answer 1

9

To elaborate on @dawehner's answers, default configuration is not cached.

It is only imported when you install a module. After that, if you make changes to default configuration, the only way to add it to existing installations is to write an update function that adds it manually.

During development, there are modules like Configuration development, that can automate that process, but they will obviously not be present on any productive installations.

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.