¿Cómo puedo crear un formulario para crear artículos en Joomla?
Estoy probando ChronoForms 8, para diseñar un formulario que pueda crear artículos y no encuentro documentación adecuada. Con la ayuda de Gemini, he obtenido ciertas indicaciones y me ha dado un código php, pero me da un error:
Código PHP:
// Asegura que el entorno de Joomla! esté cargado. Si no, detiene la ejecución.defined('_JEXEC') or die;
// --- INICIO: Definición MANUAL y EXPLÍCITA de la RUTA BASE del FRONTEND ---$joomlaRootPath = '/volume1/Web/AyPlan'; // <-- ¡¡VERIFICA Y AJUSTA ESTA RUTA SI ES DIFERENTE!!// --- FIN: Definición MANUAL y EXPLÍCITA de la RUTA BASE del FRONTEND ---
echo "DEBUG: La ruta raíz de Joomla! que usaremos es: " . $joomlaRootPath . "<br>";
// Paso CRÍTICO: Inicializar el entorno de Joomla! y el autoloader.if (!class_exists('Joomla\\CMS\\Factory')) { $frameworkPath = $joomlaRootPath . '/includes/framework.php'; if (!file_exists($frameworkPath)) { die("ERROR FATAL: El archivo framework.php NO se encuentra en la ruta: " . $frameworkPath . ". La instalación de Joomla! es incompleta o la ruta es incorrecta."); } require_once $frameworkPath; echo "DEBUG: framework.php cargado.<br>";} else { echo "DEBUG: Joomla\\CMS\\Factory ya estaba cargado.<br>";}
// Verificar si el autoloader de Joomla está listo para la clase ContentTable.if (!class_exists('Joomla\\CMS\\Table\\ContentTable')) { $pathToContent = $joomlaRootPath . '/libraries/src/Table/Content.php'; echo "DEBUG: Intentando cargar el archivo de clase directamente: " . $pathToContent . "<br>"; if (!file_exists($pathToContent)) { die("ERROR FATAL: El archivo Content.php NO se encuentra en la ruta: " . $pathToContent . ". Revisa la variable \$joomlaRootPath y los permisos del archivo."); } require_once $pathToContent; echo "DEBUG: Content.php forzado a cargar.<br>";} else { echo "DEBUG: La clase ContentTable ya fue encontrada por el autoloader.<br>";}
use Joomla\CMS\Factory;use Joomla\CMS\Table\ContentTable;echo "DEBUG: Clases de Joomla! 'use' declaradas.<br>";
$app = Factory::getApplication();$jinput = $app->input;echo "DEBUG: Aplicación y JInput obtenidos.<br>";
$form_data = $this->data;echo "DEBUG: Datos del formulario obtenidos.<br>";
$article_title = $form_data['titulo_del_plan'];$article_fulltext = $form_data['describe_tu_plan'];$article_introtext = '';$category_id = 8; // <-- ¡¡CAMBIA ESTE NÚMERO POR EL ID REAL DE TU CATEGORÍA!!$article_state = 1;
$user = Factory::getUser();$created_by_user_id = $user->id;if ($created_by_user_id == 0) { $created_by_user_id = 787; // <-- ¡CAMBIA ESTE ID por el ID de un usuario por defecto válido en tu Joomla!}echo "DEBUG: Datos de artículo y usuario preparados.<br>";
$current_date = Factory::getDate()->toSql();
$table = new ContentTable(Factory::getDbo());echo "DEBUG: Instancia de ContentTable creada.<br>";
$data = array( 'title' => $article_title, 'alias' => Factory::getApplication()->stringURLSafe($article_title), 'introtext' => $article_introtext, 'fulltext' => $article_fulltext, 'catid' => $category_id, 'state' => $article_state, 'created_by' => $created_by_user_id, 'created' => $current_date, 'language' => '*',);echo "DEBUG: Datos del artículo asignados.<br>";
if (!$table->bind($data)) { $form->errors[] = 'No se pudo vincular los datos del artículo: ' . $table->getError(); return false;}echo "DEBUG: Artículo vinculado.<br>";
if (!$table->check()) { $form->errors[] = 'Error de comprobación del artículo: ' . $table->getError(); return false;}echo "DEBUG: Artículo comprobado.<br>";
if (!$table->store(true)) { $form->errors[] = 'No se pudo guardar el artículo: ' . $table->getError(); return false;}echo "DEBUG: Artículo guardado.<br>";
$form->data['new_article_id'] = $table->id;$app->enqueueMessage('Artículo creado con éxito!', 'message');
return true;?>
..............................................................
Este es el error que me sale al cargar el formulario:
Se ha producido un error.0Class "Joomla\CMS\Table\ContentTable" not found
¿Pueden ayudarme?
Hi Victor
You need to use the Save Data action to create a new entry in the Joomla #__content table, this is easy, you just need to check which table columns you need to save data into, you can use the Modify Data Source behavior to set specific values for specific columns
This is explained here:
You can always use {data:field-name} to use the value of a form field.
I have already managed to save the form content to the database. It was difficult because I had to add the "Actions" in the "Submit" tab, and I was putting it in the "Load" tab. They should explain what the "Load" and "Submit" tabs are for and how to use each one.
Now I need to ensure that the content of each table is saved correctly, according to the required format for Joomla articles. For example:
-
Can the asset_id table be generated?
-
I believe the "alias" table needs to save the content of the "title" field, but in lowercase and with spaces replaced by hyphens. It should not have accents, the letter 'ñ', or special characters (ç, %, &, !, @, etc.). It can only contain letters (a-z), numbers (0-9), and hyphens (-). It should not start or end with a hyphen. Is this possible?
Example:
-
Title: Mi Evento Especial 2025
-
Generated/Permitted Alias: mi-evento-especial-2025
Thanks!