I have been trying to figure out how to decode ( also try to encode) my YML file so I can have full control over on PHP array. I try this...

enter image description here

And as you can see, I did not get the information inside of the YML file.

My question is what I'm doing wrong ? Why is not showing me the information inside of /core/modules/toolbar/toolbar.breakpoints.yml file?

Here is code snippet...

use Drupal\Component\Serialization\Yaml;

$breakpointEncoded = Yaml::encode('/core/modules/toolbar/toolbar.breakpoints.yml');
  • Hi Darol - I think that function needs actual YAML, so you'd have to read the file in first. That said, I think there is a core service that can do both. – cilefen Apr 27 '16 at 14:56
  • I think there's only api.drupal.org/api/drupal/core%21core.services.yml/service/… @cilefen, which proxies straight through to the same class. I don't think there's a core method that will also load the file contents for you – Clive Apr 27 '16 at 14:59
  • 1
    "think" as in, "I think I saw that at a sprint three years ago", so goes the memory... – cilefen Apr 27 '16 at 15:02

The arg to decode is:

The raw data string to decode

So you need (e.g.):

$breakpointEncoded = Yaml::decode(file_get_contents('/PATH/TO/core/modules/toolbar/toolbar.breakpoints.yml'));
  • I haven't really been digging through the Yaml class, but what happens if file_get_contents() returns FALSE i.e. if the path is wrong or not readable? Wouldn't that trigger an exception quite easily? :) What about storing the result of file_get_contents() and checking it for false before calling Yaml::decode() might help others avoid possible crashes. – hampusn Apr 27 '16 at 17:04
  • Absolutely @hampusn - this is just a very basic sample snippet, I'll leave integrating around business logic/exception handling to the user :) – Clive Apr 27 '16 at 17:07
  • Fair point @Clive! I'm just hoping no copy/pasters take this to production :D – hampusn Apr 27 '16 at 17:13
  • They've got to learn @hampusn ;) – Clive Apr 27 '16 at 17:14

You can use the Symfony component as well in Drupal 8:

use Symfony\Component\Yaml\Yaml;    

$file_path = DRUPAL_ROOT . '/core/modules/toolbar/toolbar.breakpoints.yml';
$file_contents = file_get_contents($file_path);
$ymldata = Yaml::parse($file_contents);
  • 1
    Since Drupal 8.4 (using symphony 3.0) Yaml::Parse no longer takes a file name, so only the second variant works. – AdamS Jan 22 '18 at 11:15
  • Thanks, I have removed the first variant where it passed in a file name. – oknate Jan 22 '18 at 16:49

Even simplier now with a one-liner on Drupal 8.9 (the decode function has been removed from the Yaml Symfony Dependency utility class) :

use Symfony\Component\Yaml\Yaml; 

$ymldata = Yaml::parseFile(DRUPAL_ROOT . '/core/modules/toolbar/toolbar.breakpoints.yml');
  • You really need to specify WHICH Yaml class you're talking about. When you mention "Drupal 8.9" it sounds like you're talking about the Drupal Yaml class and that you're telling us about a change that was made in Drupal since the previous answers. That's not true. The Drupal Yaml class did not remove decode(), and does not have a parseFile() method. You must be talking about the Symfony class then - please add the appropriate "use" statement to your answer. – anonymous Nov 3 '20 at 19:18
  • Yes, sorry about that. I edit the answer. – Simon Vart Nov 4 '20 at 8:46

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.