You need to be logged in to post in the forum - Log In

An active JCE Pro Subscription is required to post in the forum - Buy a Subscription

Support is currently Offline

Official support hours
Monday to Friday
09:00 - 17:00 Europe/London (BST)

Please create a new Ticket and we will get back to you as soon as we can.

#114664 Tangentially Related Question

Posted in ‘Editor’
This is a public ticket

Everybody will be able to see its contents. Do not include usernames, passwords or any other sensitive information.

Latest post by Ryan on Friday, 15 September 2023 19:36 BST

bascherz
I am trying to develop a plugin that does nothing other than trigger the Joomla event onContentAfterSave. Assuming JCE does this at some point, I went looking for that code as a model, but I cannot figure it out. As an act of desperation, I thought I'd reach out to you directly for some help. I've already reached out to the folks at Joomla Stack Exchange here. I've tried several simplistic approaches, none of which have worked as yet. Hoping (since I am paid subscriber to JCE) that you folks might have some suggestions. I know it's not about JCE, but I figured someone there might be willing to lend me a hand.

Please let me know if you cannot access the link above and I will provide more context and detail.

Thanks in advance.
Bruce

Ryan
I'm not famlilar with the J4 Task Schedular so I asked ChatGPT to generate a Task Schedular plugin for this, using the following prompt: "Write a Joomla 4 Task Schedular task that gets the oldest unpublished article whose title includes the word "test" and whose publish date is in the future, then if an article exists, load it and publish it, and trigger the onContentAfterSave event with the context of 'com_content.article'." The result was: a. Plugin Manifest: testarticlepublish.xml

<?xml version="1.0" encoding="utf-8"?>
<extension version="4.0" type="plugin" group="task" method="upgrade">
    <name>plg_task_testarticlepublish</name>
    <author>Author Name</author>
    <creationDate>2023-09-15</creationDate>
    <version>1.0.0</version>
    <description>Description of the plugin</description>
    <files>
        <filename plugin="testarticlepublish">testarticlepublish.php</filename>
    </files>
</extension>
b. Plugin File: testarticlepublish.php

<?php
defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\CMS\Plugin\CMSPlugin;

class PlgTaskTestarticlepublish extends CMSPlugin
{
    public function onTaskExecute($taskContext, $params)
    {
        // Ensure this task is for us
        if ($taskContext !== 'plg_task_testarticlepublish')
        {
            return;
        }

        $db = Factory::getDbo();
        $query = $db->getQuery(true)
            ->select('*')
            ->from($db->quoteName('#__content'))
            ->where($db->quoteName('state') . ' = 0') // Unpublished
            ->where($db->quoteName('title') . ' LIKE ' . $db->quote('%test%')) // Title contains "test"
            ->where($db->quoteName('publish_up') . ' > ' . $db->quote(Factory::getDate()->toSql())) // Publish date in the future
            ->order('created ASC')
            ->setLimit(1);

        $article = $db->setQuery($query)->loadObject();

        if ($article)
        {
            $article->state = 1; // Set to published
            $db->updateObject('#__content', $article, 'id');

            // Trigger the onContentAfterSave event
            Factory::getApplication()->triggerEvent('onContentAfterSave', ['com_content.article', &$article, false]);
        }
    }
}
Perhaps you might find this useful?

Ryan Demmer

Lead Developer / CEO / CTO

Just because you're not paranoid doesn't mean everybody isn't out to get you.

Ryan
Here is the same as a CLI script:

<?php

// We are a valid entry point.
const _JEXEC = 1;

if (!defined('JPATH_CLI'))
{
    define('JPATH_CLI', __DIR__);
}

require dirname(__DIR__) . '/includes/defines.php';
require dirname(__DIR__) . '/includes/framework.php';

use Joomla\CMS\Factory;
use Joomla\CMS\Application\CliApplication;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class PublishTestArticleCommand extends Command
{
    protected static $defaultName = 'article:publish-test';

    protected function configure()
    {
        $this->setDescription('Publish the oldest unpublished article with "test" in the title and future publish date.');
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $db = Factory::getDbo();
        $query = $db->getQuery(true)
            ->select('*')
            ->from($db->quoteName('#__content'))
            ->where($db->quoteName('state') . ' = 0') // Unpublished
            ->where($db->quoteName('title') . ' LIKE ' . $db->quote('%test%')) // Title contains "test"
            ->where($db->quoteName('publish_up') . ' > ' . $db->quote(Factory::getDate()->toSql())) // Publish date in the future
            ->order('created ASC')
            ->setLimit(1);

        $article = $db->setQuery($query)->loadObject();

        if ($article)
        {
            $article->state = 1; // Set to published
            $db->updateObject('#__content', $article, 'id');

            // Use the new event dispatching system
            $dispatcher = Factory::getContainer()->get('dispatcher');
            $event = new \Joomla\CMS\Event\ContentAfterSave('onContentAfterSave', ['context' => 'com_content.article', 'item' => $article, 'isNew' => false]);
            $dispatcher->dispatch('onContentAfterSave', $event);

            $output->writeln('<info>Article published successfully!</info>');
        }
        else
        {
            $output->writeln('<comment>No matching article found.</comment>');
        }

        return Command::SUCCESS;
    }
}

// Create the application.
$cli = CliApplication::getInstance('PublishTestArticleCommand');
$cli->addCommand(new PublishTestArticleCommand);
$cli->execute();

Ryan Demmer

Lead Developer / CEO / CTO

Just because you're not paranoid doesn't mean everybody isn't out to get you.

bascherz
Interesting approach. I have not tried ChatGPT for anything serious yet. Interestingly, the two examples above are just variations of what I've been doing all along. The CLI example does add a command, but I don't think the ->execute method executes the command. I think it executes adding the command to the application. Both examples more or less confirm that my code is on the right track.

The first example does create a plugin extension, which my external script doesn't do. However, since the code will eventually run inside a Community Builder Auto Action (which is itself a plugin), that context is definitely there.

Still scratching my head...

Thanks, Ryan
Bruce

Ryan
Interesting approach. I have not tried ChatGPT for anything serious yet. Interestingly, the two examples above are just variations of what I've been doing all along. The CLI example does add a command, but I don't think the ->execute method executes the command. I think it executes adding the command to the application. Both examples more or less confirm that my code is on the right track.


I use ChatGPT a lot to bootstrap stuff, especially if I don't know where to start or if there is a lot of tedious code to write. It's often wrong in parts, but it can be very helpful to start finding a solution to something.

Ryan Demmer

Lead Developer / CEO / CTO

Just because you're not paranoid doesn't mean everybody isn't out to get you.

bascherz
I might start using that more. It took me days to find the same thing on my own with Google.

Ryan
[quotePost id="114670"]It took me days to find the same thing on my own with Google.[/quotePost]

That used to be me 😉

Ryan Demmer

Lead Developer / CEO / CTO

Just because you're not paranoid doesn't mean everybody isn't out to get you.