0

I am trying to develop a hobby project based on https://www.themealdb.com/ api.

I am trying to create an article programatically for

https://www.themealdb.com/api/json/v1/1/search.php?s=Arrabiata

and I was able to create an article with

    if (!defined('_JEXEC')) {
        define('_JEXEC', 1);
        define('JPATH_BASE', realpath(dirname(__FILE__)));
        require_once JPATH_BASE . '/includes/defines.php';
        require_once JPATH_BASE . '/includes/framework.php';
        defined('DS') or define('DS', DIRECTORY_SEPARATOR);
    }
    $app = JFactory::getApplication('site');

    $url = 'https://www.themealdb.com/api/json/v1/1/search.php?s=Arrabiata';

    $response = json_decode(curlFnToGetResponseFromMealDB($url));

    $meal = $response->meals;

    $cat_mealdb_id = 233; // I have a function that gets the category
    $tags= []; // I have a function that gets the tags

    $args = [
                'title' => $meal->strMeal.'-'.uniqid(),
                'catid' => $cat_mealdb_id,
                'introtext' => '', //nl2br($meal->strInstructions),
                'fulltext' => '', //nl2br($meal->strInstructions),
                'tags' => $tags

            ];

            $article_id = prepareArticle($args)

    function prepareArticle($args){
        $article = array(
            'id' => isset($args['id']) && !empty($args['id'])?$args['id']:NULL,
            'catid' => isset($args['catid']) && !empty($args['catid'])?$args['catid']:NULL,
            'title' =>  isset($args['title']) && !empty($args['title'])?$args['title']:NULL,
            'alias' =>  isset($args['alias']) && !empty($args['alias'])?$args['alias']:NULL,
            'introtext' => isset($args['introtext']) && !empty($args['introtext'])?$args['introtext']:NULL,
            'fulltext' => isset($args['fulltext']) && !empty($args['fulltext'])?$args['fulltext']:NULL,
            'state' => 1,
            'language' => '*',
        'tags' => isset($args['tags']) && !empty($args['tags'])?$args['tags']:[],

        );
        $article_id = createArticle($article);
        return $article_id;
    }


    function createArticle($data)
    {
        $data['rules'] = array(
            'core.edit.delete' => array(),
            'core.edit.edit' => array(),
            'core.edit.state' => array(),
        );

        $basePath = JPATH_ADMINISTRATOR . '/components/com_content';
        require_once $basePath . '/models/article.php';
        $config = array();
        $article_model = new ContentModelArticle($config);
        if (!$article_model->save($data)) {
            $err_msg = $article_model->getError();
            return false;
        } else {
            $id = $article_model->getItem()->id;
            return $id;
        }
    }

This is working well. I was able to create an article. Now i need to create custom fields based on the meal.

In this case

  1. Create a field with name penne rigate, if not exists // penne rigate is the strIngredient1

  2. Add the value of strMeasure1 // = 1 pound to that field

  3. Map the field to this created article

How to create a article field and map it to the article.

Alaksandar Jesus Gene
  • 5,522
  • 10
  • 43
  • 74
  • Please join [joomla.se] Stack Exchange and ask all of your Joomla questions there to read a community with an intimate understanding of the CMS and its extensions. Before you post this question there, please read https://stackoverflow.com/a/4559976/2943403 and update your code to remove this very common anti-pattern. It might be worth reading about the "null coalescing operator" as well. – mickmackusa Jul 19 '21 at 23:24

0 Answers0