$table->get( '_location_id' )

68 views
Skip to first unread message

JOOMLATEMA.NET

unread,
Dec 7, 2022, 8:52:32 AM12/7/22
to joomla-de...@googlegroups.com
Hi everyone. I am trying to adapt a j3 content plugin to j4. It copies menu item with module assignments. I could not figure out what that '_location_id'   refers to. Regards. 
// Find all assigned modules
        $query1 = $this->db->getQuery(true)
->select($this->db->quoteName('moduleid'))
->from($this->db->quoteName('#__modules_menu'))
->where($this->db->quoteName('menuid') . ' = ' . $table->get( '_location_id' ) );
$this->db->setQuery($query1);
        $modules = (array) $this->db->loadColumn();

        // Assing all found modules to copied menu item
        if( !empty( $modules ) )
        {
            foreach( $modules as $mid )
            {
                $mdl = new stdClass();
                $mdl->moduleid = $mid;
                $mdl->menuid = $table->get( 'id' );
                $this->db->insertObject( '#__modules_menu', $mdl );
            }
        }

        // Check if menu item is on the assign to all except list
        $query2 = $this->db->getQuery(true)
->select($this->db->quoteName('moduleid'))
->from($this->db->quoteName('#__modules_menu'))
->where($this->db->quoteName('menuid') . ' = -' . $table->get( '_location_id' ) );
$this->db->setQuery($query2);
        $modulesExcept = (array) $this->db->loadColumn();

        // Add menu item to the exception list for all modules that have the original one in there
        if( !empty( $modulesExcept ) )
        {
            foreach( $modulesExcept as $mid )
            {
                $mdl = new stdClass();
                $mdl->moduleid = $mid;
                $mdl->menuid = $table->get( 'id' ) * -1;
                $this->db->insertObject( '#__modules_menu', $mdl );
            }
        }

return true;
}
}

Mark Stanton

unread,
Dec 8, 2022, 3:41:21 AM12/8/22
to Joomla! General Development
What is $table in this code?

Of course the easy guess is it's the table class for the modules menu table, but neither the J3 nor J4 version has that field (and it would be very odd to be named with a leading underscore even if I'm looking in the wrong place).

Because of this, I think _location_id is something from the plugin, not core J!

JOOMLATEMA.NET

unread,
Dec 8, 2022, 1:49:19 PM12/8/22
to joomla-de...@googlegroups.com
Hi Mark thanks for the answer. This is the plugin's Github page: https://github.com/alexxandar/Copy-Module-Assignments
It extends JPlugin. It seems that $table is just a variable of function. Here is the copymuduleassignment.php

<?php
/**
 * @package     Joomla.Plugin
 * @subpackage  Content.CopyAssignments
 *
 * @copyright   Copyright (C) 2016 Aleksandar Jovanović (him...@alexxandar.me). All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

/**
 * Copy Module Assignments Plugin
 */
class PlgContentCopymoduleassignments extends JPlugin
{
/**
* Database object
*
* @var    JDatabaseDriver
* @since  3.3
*/
protected $db;

/**
* Plugin that copies module assignments from original to new menu item when using "Save as copy"
*
* @param   string   $context  The context of the content being passed to the plugin.
* @param   mixed    &$row     An object with a "text" property
* @param   mixed    $params   Additional parameters. See {@see PlgContentContent()}.
* @param   integer  $page     Optional page number. Unused. Defaults to zero.
*
* @return  boolean True on success.
*/
public function onContentAfterSave( $context, &$table, $isNew )
{
   // Return if invalid context
if ( $context != 'com_menus.item' )
return true;

        // Return if items is not a product of "Save as copy"
        if ( !( $table->get( '_location_id' ) > 1 && $isNew == false ) )
            return true;
--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/joomla-dev-general/2145b368-8491-4dc0-8fcd-06977fdb6b61n%40googlegroups.com.

Mark Stanton

unread,
Dec 8, 2022, 2:51:53 PM12/8/22
to Joomla! General Development
Yup, but doesn't really help as $table is passed in there, and *that* doesn't help because the header comment is (significantly) wrong.  Looks like it might be a core J! thing, I'm not familiar with this part of the code.  Trace the running (ie J3) code to see what it is.

JOOMLATEMA.NET

unread,
Dec 9, 2022, 3:08:33 AM12/9/22
to joomla-de...@googlegroups.com
okay thank yo uvery much

Mark Fleeson

unread,
Feb 9, 2023, 12:44:03 PM2/9/23
to Joomla! General Development
Have you tested that the original github code works in Joomla 3?
 If so then here's your code updated to Joomla 4 -

<?php
/**
* @package Joomla.Plugin
* @subpackage Content.CopyAssignments
*
* @copyright Copyright (C) 2016 Aleksandar Jovanović (him...@alexxandar.me). All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\Database\DatabaseDriver;

defined('_JEXEC') or die;

/**
* Copy Module Assignments Plugin
*
* @since 4.0.0
*/
class PlgContentCopymoduleassignments extends CMSPlugin
{
/**
* Database object
*
* @var DatabaseDriver
* @since 3.3
*/
protected $db;

/**
* Plugin that copies module assignments from original to new menu item when using "Save as copy"
*
* @param string $context The context of the content being passed to the plugin.
* @param $table
* @param $isNew
*
* @return boolean True on success.
*
* @since 4.0.0
*/
public function onContentAfterSave(string $context, $table, $isNew): bool
{
// Return if invalid context
if ($context != 'com_menus.item')
return true;

// Return if items is not a product of "Save as copy"
if (!($table->get('_location_id') > 1 && $isNew == false))

JOOMLATEMA.NET

unread,
Feb 9, 2023, 2:02:17 PM2/9/23
to joomla-de...@googlegroups.com
Hi Mark
Thanks for the answer and the help J3 Version works on J3 I have been using it for a while. I have replaced the code but it does  not worked on J4 am I missing something? Thanks.

9 Şub 2023 Per 20:44 tarihinde Mark Fleeson <mfle...@gmail.com> şunu yazdı:

Hannes Papenberg

unread,
Feb 10, 2023, 1:52:29 AM2/10/23
to Joomla! General Development
Don't set _location_id directly. Use Table::setLocation() instead.

JOOMLATEMA.NET

unread,
Feb 10, 2023, 1:39:07 PM2/10/23
to joomla-de...@googlegroups.com
Thanks for the answer I do not have an advanced php programming capacity. Please give me a bit more details regards

Reply all
Reply to author
Forward
0 new messages