6

I am using the Business Hours module. It allows you to specify two time periods per day, for the 7 days of the week. So you could, for example, say: 9 t 1 Mondays to Fridays, and 2 - 5, Mondays to Fridays.

My problem is getting access to these values as I need to expose it in a custom module. With print_r($node), I can see the following.

[field_office_hours] => Array(
  [x-default] => Array(
    [0] => Array(
      [day] => 0
      [starthours] => 900
      [endhours] => 1700
    )
    [1] => Array(
      [day] => 1
      [starthours] => 900
      [endhours] => 1700
    )
    /* Omissis */
  )
)

I have no idea how to get this as array. I know that the module requires the "Date" module, so these are probably Date fields, but how do I actually get the values as array?

$node->field_office_hours->value doesn't work.

8

Nevermind, this worked for me:

$node->get('field_office_hours')->getValue();
  • 1
    That gives you an array of all the values. It's $node->get('field_office_hours')->$property_name. the property name is often but not always value. You see he properties in your dump, so just use ->day and so on instead of value. – Berdir Jun 18 '16 at 18:16

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.