1

I added some custom fields to the articles of my Joomla website: enter image description here I work on a custom Joomla module that creates new articles programmatically using the below code:

static function createArticle(GNGLocation $loc, GNGLang $lang, $title, $alias, $intro, $summary, $imageStr) {

$table = JTable::getInstance('Content', 'JTable', array());

$jcat = $loc->get('jcat_id_tour');
$jlang = $lang->get('joomlaName');

$data = array(
    'catid' => $jcat,
    'title' => $title,
    'alias' =>  $alias,
    'language' => $jlang,
    'introtext' => $summary,
    'fulltext' => $intro,
    'state' => 0,
);

// Bind data
if (!$table->bind($data))
{
    throw new Exception("Failed to bind article data. Error: " . $table->getError());
}

// Check the data.
if (!$table->check())
{
    throw new Exception("Failed to check article data. Error: " . $table->getError());
}

// Store the data.
if (!$table->store())
{
    throw new Exception("Failed to store article data. Error: " . $table->getError());
    }


}

This works fine, creates the new article, but I could not find a way to also write the values of the custom fields that belong to the article. This is what I tried so far, based on this post, but it does not seem to work, meaning that when I go to the article in the backend and check the custom fields they are still empty. No error message or whatsoever:

    JModelLegacy::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_fields/models');

    $model =& JModelLegacy::getInstance('Article', 'ContentModel', array('ignore_request'=>true));
    $appParams = JFactory::getApplication()->getParams();
    $model->setState('params', $appParams);
    $item =& $model->getItem($table->id);


    $custom_fields = FieldsHelper::getFields('com_content.article',  $item, True);
    $custom_fields_by_name = \Joomla\Utilities\ArrayHelper::pivot($custom_fields, 'name');
    $your_custom_field_value = 'haliho';

    $model_field = JModelLegacy::getInstance('Field', 'FieldsModel', ['ignore_request' => true]);
    $model_field->setFieldValue(
        $custom_fields_by_name['excluded']->id,
        $item->id,
        $your_custom_field_value
    );

UPDATE: running the following line, gives the below diagnostics:

JFactory::getApplication()->enqueueMessage("<pre>" . json_encode($custom_fields_by_name, JSON_PRETTY_PRINT). "</pre><br>" . $custom_fields_by_name['excluded']->id . "<br>" . $item->id , 'info');

Output:

info
{
    "included": {
        "id": "2",
        "title": "Included",
        "name": "included",
        "checked_out": "0",
        "checked_out_time": "0000-00-00 00:00:00",
        "note": "",
        "state": "1",
        "access": "1",
        "created_time": "2019-11-24 16:10:18",
        "created_user_id": "919",
        "ordering": "0",
        "language": "*",
        "fieldparams": {
            "filter": "",
            "maxlength": ""
        },
        "params": {
            "hint": "",
            "class": "",
            "label_class": "",
            "show_on": "",
            "render_class": "",
            "showlabel": "1",
            "label_render_class": "",
            "display": "0",
            "layout": "",
            "display_readonly": "2"
        },
        "type": "text",
        "default_value": "",
        "context": "com_content.article",
        "group_id": "1",
        "label": "Included",
        "description": "",
        "required": "0",
        "language_title": null,
        "language_image": null,
        "editor": null,
        "access_level": "Public",
        "author_name": "Administrator",
        "group_title": "Tour Extension",
        "group_access": "1",
        "group_state": "1",
        "group_note": "",
        "value": "",
        "rawvalue": ""
    },
    "excluded": {
        "id": "3",
        "title": "Excluded",
        "name": "excluded",
        "checked_out": "0",
        "checked_out_time": "0000-00-00 00:00:00",
        "note": "",
        "state": "1",
        "access": "1",
        "created_time": "2019-12-01 14:40:45",
        "created_user_id": "919",
        "ordering": "0",
        "language": "*",
        "fieldparams": {
            "filter": "",
            "maxlength": ""
        },
        "params": {
            "hint": "",
            "class": "",
            "label_class": "",
            "show_on": "",
            "render_class": "",
            "showlabel": "1",
            "label_render_class": "",
            "display": "0",
            "layout": "",
            "display_readonly": "2"
        },
        "type": "text",
        "default_value": "",
        "context": "com_content.article",
        "group_id": "1",
        "label": "Excluded",
        "description": "",
        "required": "0",
        "language_title": null,
        "language_image": null,
        "editor": null,
        "access_level": "Public",
        "author_name": "Administrator",
        "group_title": "Tour Extension",
        "group_access": "1",
        "group_state": "1",
        "group_note": "",
        "value": "",
        "rawvalue": ""
    }
}
3
332

What am I doing wrong, how should I do this?

1

I suspect it's because you are running the code under a Joomla user who doesn't have permission to set values for that 'excluded' custom field. Within the Joomla field model code for setFieldValue() there's a call to FieldsHelper::canEditFieldValue($field) and if the Edit Custom Field Value permission for your user's group is not set to Allowed then the setFieldValue() function will return false, but not output an error message.

I suggest you try setting Public Allowed access on the action Edit Custom Field Value for this field, and see if that solves the problem. If you don't want to set this permanently like this, then you could override the setFieldValue() method, using your own code which omits the permissions check.

I ran the code on my own instance, and it worked fine after I set the permission. And walking through the associated Joomla code with a debugger I didn't see any other lines that might have caused the problem.

  • Thanks, it worked like a charm! – wanderlusted Dec 14 '19 at 10:24

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.