0

When creating a new article, how can I get the current selected category ID as shown in the category select field? I need to pass the value to a PHP variable.

There is the javascript function categoryHasChanged(element) in the page at administrator/index.php?option=com_content&view=article&layout=edit which is getting the category ID from a PHP function, I just haven't determined how.

function categoryHasChanged(element) {
    var cat = jQuery(element);
    if (cat.val() == '2')return;
    Joomla.loadingLayer('show');
    jQuery('input[name=task]').val('article.reload');
    Joomla.submitform('article.reload', element.form);
}
jQuery( document ).ready(function() {
    Joomla.loadingLayer('load');
    var formControl = '#jform_catid';
    if (!jQuery(formControl).val() != '2'){jQuery(formControl).val('2');}
});

The function is used in the category select field in the onchange="" attribute

<select id="jform_catid" name="jform[catid]" data-custom_group_text="New Categories" data-no_results_text="Add new Category" data-placeholder="Type or Select a Category" data-custom_value_prefix="#new#" class="required chzn-custom-value" required aria-required="true" onchange="categoryHasChanged(this);">
    <option value="2">Uncategorised</option>
    <option value="8">The Green Mile</option>
</select>

How is the category ID delivered for a new article?

0

If you're creating a new article then the category associated with that article is initially not going to be set - you're just going to have a category field which has the available categories from which the user can choose one.

Once the user selects a category in this category field then the onchange attribute against it results in the categoryHasChanged function being run. This causes an HTTP POST to be sent to the server with task = article.reload (for com_content) and all the html input fields which are set are sent in the HTTP POST parameters, including the category id relating to the category which the user has chosen.

(A reason for this reload is that the custom fields which are available are dependent upon the category, so if you change the category then you may need to change what custom fields are displayed).

Once the POST gets to the server the data is stored in the session using code such as

$app->setUserState($context . '.data', $data);

(eg in FormController), and then an HTTP redirect back to the original page is issued.

When the GET for the original page is next received then the data is retrieved from the session and injected into the form. (The custom fields functionality will look at the category id set in this data to work out which custom fields to include in the form).

So if you want to get at this category id in PHP, I think you should write a content plugin which triggers on onContentPrepareData where you can get at this data which has been injected into the form during this reload operation.

0

You can use the session as in the following example.

$app = JFactory::getApplication();
$stateVar = $app->getUserState( "com_content.articles.filter.category_id", array() );

Will return an array with the selected categories in the filter or an empty array if the session has not been set.

1

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.