You are here

function recipe_build_total_time in Recipe 8.2

1 call to recipe_build_total_time()
recipe_node_view in ./recipe.module
Implements hook_ENTITY_TYPE_view().

File

./recipe.module, line 156
Contains functions for Recipe node CRUD and display.

Code

function recipe_build_total_time(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
  $node_type = NodeType::load('recipe');
  $total_time = 0;
  $total_time_count = 0;
  foreach ($entity
    ->getFieldDefinitions() as $field_name => $field_definition) {
    if (!$field_definition instanceof FieldConfigInterface) {
      continue;
    }
    if ($field_definition
      ->getType() == 'integer' && $field_definition
      ->getThirdPartySetting('recipe', 'total_time') == 1 && $entity->{$field_name}->value != NULL) {
      $total_time += $entity->{$field_name}->value;
      $total_time_count++;
    }
  }

  // Do not display the total time if only one time field or none have been
  // filled out for the recipe.
  if ($total_time_count > 1) {
    $build['recipe_total_time'] = [
      '#theme' => 'recipe_total_time',
      '#label' => $node_type
        ->getThirdPartySetting('recipe', 'total_time_label'),
      '#label_display' => $node_type
        ->getThirdPartySetting('recipe', 'total_time_label_display'),
      '#total_time' => [
        '#theme' => 'recipe_duration',
        '#duration' => $total_time,
      ],
    ];
  }
}