1

I have made a simple example to practice using the configuration api in drupal 8. I created the following twitter_pull.credentials.yml file in the config/install folder:

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

and flushed my cache. In a custom block, I pulled in the configuration

$config = \Drupal::config('twitter_pull.credentials');

and when I did a dpm

dpm($config);

I did not get a null result. However, when I tried accessing a property:

drupal_set_message($config->get('consumer_key'));

now the result was null. Even when I tried using a configuration object which already exists

 $enabled = \Drupal::config('system.maintenance')->get('enabled');
dpm($enabled);

it still returned null. What am I missing here?

EDIT: If it helps anything here is the full code:


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');
  }
}

  • You haven't mentioned a config schema so read this first: drupal.org/node/1905070 – Clive Aug 18 '14 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 '14 at 14:27
  • 1
    I would assume you installed the module before creating the config file, so the config was never imported. – Daniel Wehner Aug 18 '14 at 15:21
  • @vijaycs85 - I have added the extra code. – user1015214 Aug 18 '14 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 '14 at 15:34
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.