Hi Drupal community!

Super loving D8 but still getting my head around a few things.

Mainly accessing field values from paragraph preprocess functions. Here is the bit of code I have inside my base.theme file:

function base_preprocess_paragraph(&$variables) {
	$paragraph = $variables['paragraph'];
	$variables['heading'] = $paragraph->field_heading->get(0)->value;
}

This does, indeed, give me access to this variable in my template file. (meaning {{ heading }} prints the correct value)

However, while the specific paragraph prints to the screen, nothing else get printed. Even the <head> is completely empty!

Got a few questions:
1) Is this even the right way to be accessing the field values? NOTE: I tried the getValues() method and got the same issue as above.
2) How would I manually drill down to the value?
3) Any other thoughts/best practices?

Thanks!
-=b=-

Comments

be_design created an issue. See original summary.

scottsawyer’s picture

This is an old issue, but in case you haven't found a solution, it may just be a plain PHP error. You are not checking to see if that paragraph field exists or whether it has a value before you set the variable.

function base_preprocess_paragraph(&$variables) {
	$paragraph = $variables['paragraph'];
       $variables['heading'] = '';
    if (!$paragraph->field_heading->isEmpty()) {
        // I am assuming this is a multivalue paragraph field?
        // For a single value field, I would do something like this:
        // $variables['heading'] = $paragraph->field_heading->getValue();
	$variables['heading'] = $paragraph->field_heading->get(0)->value;
   }
}
berdir’s picture

Status: Active » Fixed

Yes, sounds like a php fatal error that aborts. Always develop with verbose error logging.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.