1

The registered user gets the "Author" access rights. Through the menu, you can make the button "Create material" and the Author, having entered the site, can create this material. This is understandable.

It is necessary that the Author, by pressing a button in his profile, could create a Category, having only title and alias.

How to implement this in Joomla 4? Everything that I find is outdated and does not work.

=================================

In file: templates/mytemplate/html/com_users/profile/default.php there is a button and a script

$(".url").click(function(){
    var url = $("#basic-url").val();
    var idbtnclick1 = $(this).attr('id');
    $.ajax({
        type: 'post',
        url: "/property/profile/page.php", 
        data: {
            'user': "<?php echo $user ?>",
            'url': url,
            'command': idbtnclick1
            },
        response: 'text',
        //location.reload();
        //success: function(data){
        //  $("#eddd").html(data); 
        //}
    })
    //console.log(url);
});

File: /property/profile/page.php

define('_JEXEC', 1);
define('JPATH_BASE', realpath(dirname(__FILE__) . '/../..'));
require_once JPATH_BASE . '/includes/defines.php';
require_once JPATH_BASE . '/includes/framework.php';

$user = $_POST["user"];
$url = $_POST["url"];
$command = $_POST["command"];

if ($command === 'urlplus') {

/* ???????????????????????????????????????????????? */

}

if ($command === 'urledit') {
    $object = new stdClass();
    $object->title = $user;
    $object->alias = $url;
    $object->path = $url;
    $object->modified_user_id = 419;
    $object->modified_time = null;

    $result = JFactory::getDbo()->updateObject('#__categories', $object, 'title');
}

The part where the changes are made works. We need to do the part that creates the new category. Found an example and similar ones but don't work.

4
  • I am unclear by what you mean when you say "Stuck on php file". You are attempting to build new functionality or are asking how to create a category in J4? I find it hard to believe that J4 documentation would be outdated; maybe you mean that everything you've found is for J3 or lower. Anyhow, welcome to JSE. Please take our tour while you wait for feedback/support. Commented Dec 9, 2021 at 10:26
  • 1
    My English is not very good. :) But I tried to clarify the issue. Commented Dec 9, 2021 at 12:31
  • Mr. Bond, how do you go with creating a category, do you need any more help or info? Or is it solved?
    – Zollie
    Commented Dec 17, 2021 at 6:53
  • 1
    Thanks for the help. I have been studying Joomla for many years and have a site on this engine. But at some point I realized that the project I'm trying to create needs a little more. I haven't visited this topic in a long time because I spend time studying Python and Django. But I'll be back to Joomla soon. Commented Jan 22, 2022 at 14:21

1 Answer 1

1

This task can be approached few different ways. I would just like to give a short version of how a new Category is created in Joomla 4 - content (not a big difference in Joomla 3 actually). Besides my example below, you should also consider additional, important security steps in your code, like CSRF checking at POST request for example, also the extension_name where we submit the request from, and there might be additional exception handling at filters. So these codes should/could be improved, but basically the following is how this works) :

I would suggest you to create your own Class for this as I did in my fast test on these methods (in a custom module).

On the top of the php file we should not forget to include the base Joomla classes we will use.

\defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;

1. I added a first method that checks whether the User really has permission to create a new Category. (This is checked the same way in the Category Controller of Joomla 4)

/**
 * Method to check if you can add a new record.
 *
 * @param   array  $data  An array of input data. // we should include $this->input->get('extension', 'com_content'); I just hardcoded com_content here for testing.
 *
 * @return  boolean
 *
 * @since   1.6
 */
protected function allowAdd()
{
    $app = Factory::getApplication();
    $user = $app->getIdentity();
    $contentExtension = 'com_content';

    return ($user->authorise('core.create', $contentExtension) || count($user->getAuthorisedCategories($contentExtension, 'core.create')));
}

2. I created an addCategory() method which uses the permission checking and prepares the data from the input (POST request) and calls for saving the data in the database.

public static function addCategory()
{
    if ((new self)->allowAdd()) 
    {
        $input = Factory::getApplication()->input;
        $data = array();
        $data['title'] = $input->get('categoryname', '');
        $data['alias'] = $input->get('categoryalias', '');
        $data['extension'] = 'com_content';
        $data['parent_id'] = 1;

        (new self)->createCategory($data);
        return true;
    } 
    else
    {
        Factory::getApplication()->enqueueMessage(Text::_('MY_COMPONENT_YOU_DO_NOT_HAVE_PERMISSION'), 'error');
    }
}

3. And I added the createCategory() method which calls the Model from the Categories component of Joomla and uses its method to save the new data.

protected function createCategory($data) 
{        
    $app = Factory::getApplication();
    $model = $app->bootComponent('com_categories')
    ->getMVCFactory()->createModel('Category', 'Administrator', ['ignore_request' => true]);

    $model->save($data);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.