DrupalX

Extension
Framework

Scalar value extraction

Even extremely basic (scalar) types like strings, integers etc. are accessed in Drupal through a field item object. This object simply has a property "value" to access the raw value of that item. On the one hand, it's easy to understand why Drupal works like this: All types (the easier ones like scalar values as well as the more complex ones) derive from a single class and are thus handled in a uniform way. The scalar case is just a special one where there is only one value. This simplifies things a lot in the Drupal source code when it has to deal with arbitrary types. On the other hand these scalar types are, unfortunately, the ones used most frequently. So, from a website developers point-of-view, things quickly get quite verbose because you explicitly add ".value" to them everytime you want to get the real value:

{{ entity.myfield.value }}

Therefore, DrupalX automatically extracts the value from these scalar types. The above example can be rewritten as:

 {{ #myfield }}

You'll notice that we've omitted the ".value" at the end. This is possible because DrupalX automatically extracts the value of this scalar field. You can find more information on the syntax used here under the chapter on field access operators.

Scalar value extraction and cardinality detection, taken together, simplify checking whether a field is empty quite a lot in DrupalX:

{{ $body?:"No body defined" }}