function InstallHelper::processRecipe

Same name and namespace in other branches
  1. 8.9.x core/profiles/demo_umami/modules/demo_umami_content/src/InstallHelper.php \Drupal\demo_umami_content\InstallHelper::processRecipe()
  2. 10 core/profiles/demo_umami/modules/demo_umami_content/src/InstallHelper.php \Drupal\demo_umami_content\InstallHelper::processRecipe()
  3. 11.x core/profiles/demo_umami/modules/demo_umami_content/src/InstallHelper.php \Drupal\demo_umami_content\InstallHelper::processRecipe()

Process recipe data into recipe node structure.

Parameters

array $data: Data of line that was read from the file.

string $langcode: Current language code.

Return value

array Data structured as a recipe node.

1 call to InstallHelper::processRecipe()
InstallHelper::processContent in core/profiles/demo_umami/modules/demo_umami_content/src/InstallHelper.php
Process content into a structure that can be saved into Drupal.

File

core/profiles/demo_umami/modules/demo_umami_content/src/InstallHelper.php, line 429

Class

InstallHelper
Defines a helper class for importing default content.

Namespace

Drupal\demo_umami_content

Code

protected function processRecipe(array $data, $langcode) {
    $values = [
        'type' => 'recipe',
        // Title field.
'title' => $data['title'],
        'moderation_state' => 'published',
        'langcode' => 'en',
    ];
    // Set article author.
    if (!empty($data['author'])) {
        $values['uid'] = $this->getUser($data['author']);
    }
    // Set node alias if exists.
    if (!empty($data['slug'])) {
        $values['path'] = [
            [
                'alias' => '/' . $data['slug'],
            ],
        ];
    }
    // Save node alias
    $this->saveNodePath($langcode, 'recipe', $data['id'], $data['slug']);
    // Set field_media_image field.
    if (!empty($data['image_reference'])) {
        $values['field_media_image'] = [
            'target_id' => $this->getMediaImageId($data['image_reference']),
        ];
    }
    // Set field_summary field.
    if (!empty($data['summary'])) {
        $values['field_summary'] = [
            [
                'value' => $data['summary'],
                'format' => 'basic_html',
            ],
        ];
    }
    // Set field_recipe_category if exists.
    if (!empty($data['recipe_category'])) {
        $values['field_recipe_category'] = [];
        $tags = array_filter(explode(',', $data['recipe_category']));
        foreach ($tags as $tag_id) {
            if ($tid = $this->getTermId('recipe_category', $tag_id)) {
                $values['field_recipe_category'][] = [
                    'target_id' => $tid,
                ];
            }
        }
    }
    // Set field_preparation_time field.
    if (!empty($data['preparation_time'])) {
        $values['field_preparation_time'] = [
            [
                'value' => $data['preparation_time'],
            ],
        ];
    }
    // Set field_cooking_time field.
    if (!empty($data['cooking_time'])) {
        $values['field_cooking_time'] = [
            [
                'value' => $data['cooking_time'],
            ],
        ];
    }
    // Set field_difficulty field.
    if (!empty($data['difficulty'])) {
        $values['field_difficulty'] = $data['difficulty'];
    }
    // Set field_number_of_servings field.
    if (!empty($data['number_of_servings'])) {
        $values['field_number_of_servings'] = [
            [
                'value' => $data['number_of_servings'],
            ],
        ];
    }
    // Set field_ingredients field.
    if (!empty($data['ingredients'])) {
        $ingredients = explode(',', $data['ingredients']);
        $values['field_ingredients'] = [];
        foreach ($ingredients as $ingredient) {
            $values['field_ingredients'][] = [
                'value' => $ingredient,
            ];
        }
    }
    // Set field_recipe_instruction field.
    if (!empty($data['recipe_instruction'])) {
        $recipe_instruction_path = $this->module_path . '/default_content/languages/' . $langcode . '/recipe_instructions/' . $data['recipe_instruction'];
        $recipe_instructions = file_get_contents($recipe_instruction_path);
        if ($recipe_instructions !== FALSE) {
            $values['field_recipe_instruction'] = [
                [
                    'value' => $recipe_instructions,
                    'format' => 'basic_html',
                ],
            ];
        }
    }
    // Set field_tags if exists.
    if (!empty($data['tags'])) {
        $values['field_tags'] = [];
        $tags = array_filter(explode(',', $data['tags']));
        foreach ($tags as $tag_id) {
            if ($tid = $this->getTermId('tags', $tag_id)) {
                $values['field_tags'][] = [
                    'target_id' => $tid,
                ];
            }
        }
    }
    return $values;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.