Automatically create and article, owned by newly created user ?

6 months 1 day ago #335915 by rbuelund
Amongst other errors in the code, you were right there :-) But when I insert
use Joomla\CMS\Factory;
I get the error: syntax error, unexpected token "use"

Please Log in to join the conversation.

6 months 1 day ago #335916 by rbuelund
Ahh got it - just used:
$newapp = Joomla\CMS\Factory::getApplication();
The following user(s) said Thank You: krileon

Please Log in to join the conversation.

5 months 4 weeks ago #335943 by rbuelund
I have now managed to almost make this work. The code I run in the auto action is:
$newapp = Joomla\CMS\Factory::getApplication();
$mvcFactory = $newapp->bootComponent('com_content')->getMVCFactory();
$articleModel = $mvcFactory->createModel('Article', 'Administrator', array('ignore_request' => true));

$article = [
    'catid' => 8,
    'com_fields' => array('cvr' => '12345678', 'adresse' => 'Testadresse', 'postnr' => '8000', 'by' => 'Aarhus C', 'telefon' => '33333333', 'webshop-url' => 'https://xxxxxx.dk'),
    'alias' => '',
    'title' => '[username]',
    'introtext' => 'ee',
    'fulltext' => '[cb_description]',
    'created_by' => $user->id,
    'state' => 1,
    'attribs' => '{"article_layout":"","show_title":"","link_titles":"","show_tags":"","show_intro":"","info_block_position":"","info_block_show_title":"","show_category":"","link_category":"","show_parent_category":"","link_parent_category":"","show_associations":"","show_author":"","link_author":"","show_create_date":"","show_modify_date":"","show_publish_date":"","show_item_navigation":"","show_icons":"","show_print_icon":"","show_email_icon":"","show_vote":"","show_hits":"","show_noauth":"","urls_position":"","alternative_readmore":"","article_page_title":"","show_publishing_options":"","show_article_options":"","show_urls_images_backend":"","show_urls_images_frontend":""}',
   'metadata' => '{"robots":"","author":"","rights":"","xreference":""}',
    'language' => '*'
];

        // Make sure that the article gets a unique alias before saving
        if ($article['alias'] == null || empty($article['alias']))
        {
            if (Joomla\CMS\Factory::getConfig()->get('unicodeslugs') == 1)
            {
                $article['alias'] = JFilterOutput::stringURLUnicodeSlug($article['title']);
            }
            else
            {
                $article['alias'] = JFilterOutput::stringURLSafe($article['title']);
            }

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

            while ($table->load(array('alias' => $article['alias'], 'catid' => $article['catid'])))
            {
                    $article['alias'] = Joomla\String\StringHelper::increment($article['alias'], 'dash');
            }
        }

if (!$articleModel->save($article)){
    throw new Exception($articleModel->getError());
}


The article is created just fine, but the custom field values are not entered - UNLESS I change the permissions "Edit custom value" on the custom field group to Public !
How can I make teh Auto action - or the batch run as the registered user (or the super user) ?

Please Log in to join the conversation.

5 months 3 weeks ago #335948 by krileon
The CB Auto Action will always run as the authenticated user. Newly registered users aren't authenticated. They're public. So the permissions check by Joomla is working correctly. I've no idea how to workaround that limitation in Joomla beyond doing what you've already done and set the permission to public.


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.

Please Log in to join the conversation.

Moderators: beatnantkrileon
Time to create page: 0.199 seconds

Facebook Twitter LinkedIn