The Joomla! Forum ™



Forum rules


Please use the mailing list here: http://groups.google.com/group/joomla-dev-general rather than this forum.



Post new topic Reply to topic  [ 12 posts ] 
Author Message
PostPosted: Thu Apr 26, 2012 3:07 pm 
Joomla! Enthusiast
Joomla! Enthusiast

Joined: Thu Oct 30, 2008 7:27 pm
Posts: 149
How do you prevent Joomla from creating a link in the Components dropdown menu for a component? I can't seem to figure it out and the components in Joomla that are like this (like com_wrapper and com_finder) didn't offer any clues.

Thanks for any help!


Last edited by drewgg on Fri Apr 27, 2012 2:42 pm, edited 1 time in total.

Top
 Profile  
 
PostPosted: Thu Apr 26, 2012 4:56 pm 
User avatar
Joomla! Apprentice
Joomla! Apprentice

Joined: Tue Apr 24, 2012 8:49 am
Posts: 41
Go to database in #_menu in type admin links see your component and remove that line.

_________________
Joomla Enterprise Projects Manager and Developer
JoomlaPT! administrator


Top
 Profile  
 
PostPosted: Thu Apr 26, 2012 5:33 pm 
Joomla! Enthusiast
Joomla! Enthusiast

Joined: Thu Oct 30, 2008 7:27 pm
Posts: 149
Thanks, but isn't there a more proper way (ie, something that wouldn't require the user who is installing the component to make manual changes to the database)? I guess I would try and make deleting that #__menu row as part of the install script but I'm not sure if that's possible (since it depends on whether the #__menu table is populated before or after the install script ends).


Top
 Profile  
 
PostPosted: Thu Apr 26, 2012 10:00 pm 
User avatar
Joomla! Apprentice
Joomla! Apprentice

Joined: Tue Apr 24, 2012 8:49 am
Posts: 41
You can use http://docs.joomla.org/Access_Control_L ... 5/Tutorial

If the component don´t have acl in options you can add using the file config.xml in base of component.

_________________
Joomla Enterprise Projects Manager and Developer
JoomlaPT! administrator


Top
 Profile  
 
PostPosted: Fri Apr 27, 2012 2:42 pm 
Joomla! Enthusiast
Joomla! Enthusiast

Joined: Thu Oct 30, 2008 7:27 pm
Posts: 149
Thank you for the additional suggestion, codigoaberto.

I ended up reading through Joomla's install script (JInstallerComponent) and confirmed that there is no way to prevent the menu from being created like I had wanted. However I was able to confirm that the postflight method of the install script is triggered after the menu item is created. I created an install script that'll remove the menu item when postflight is called. I'm attaching it here for the benefit of anyone who wants it (instructions on using the install script can be found here: Developing a Model-View-Controller (MVC) Component for Joomla!2.5 - Part 15):


Code:
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
 
/**
 * Script file of com_somecomponent component
 */
class com_somecomponentInstallerScript
{
   /**
    * method to run after an install/update/uninstall method
    *
    * @return void
    */
   function postflight($type, $parent)
   {
      $componentName = $parent->get('manifest')->name;

      $extIds = $this->getExtensionIds($componentName);

      if(count($extIds)) {
         foreach($extIds as $id) {
            if(!$this->removeAdminMenus($id)) {
               echo "notice: failed to remove the component menu link<br>";
            }
         }
      }
   }
   
   /**
   * Retrieves the #__extensions IDs of a component given the component name (eg "com_somecomponent")
   *
   * @param   string   $component The component's name
   * @return   array   An array of component IDs
   */
   protected function getExtensionIds($component) {
      $db = JFactory::getDbo();
      $query = $db->getQuery(true);
      $query->select('extension_id');
      $query->from('#__extensions');
      $cleanComponent = filter_var($component, FILTER_SANITIZE_MAGIC_QUOTES);
      $query->where($query->qn('name') . ' = ' . $query->quote($cleanComponent));
      $db->setQuery($query);
      $ids = $db->loadResultArray();
      return $ids;
   }
   
   /**
   * Removes the admin menu item for a given component
   *
   * This method was pilfered from JInstallerComponent::_removeAdminMenus()
   *
   * @param   int      $id The component's #__extensions id
   * @return   bool   true on success, false on failure
   */
   protected function removeAdminMenus(&$id)
   {
      // Initialise Variables
      $db = JFactory::getDbo();
      $table = JTable::getInstance('menu');
      // Get the ids of the menu items
      $query = $db->getQuery(true);
      $query->select('id');
      $query->from('#__menu');
      $query->where($query->qn('client_id') . ' = 1');
      $query->where($query->qn('component_id') . ' = ' . (int) $id);

      $db->setQuery($query);

      $ids = $db->loadColumn();

      // Check for error
      if ($error = $db->getErrorMsg())
      {
         return false;
      }
      elseif (!empty($ids))
      {
         // Iterate the items to delete each one.
         foreach ($ids as $menuid)
         {
            if (!$table->delete((int) $menuid))
            {
               return false;
            }
         }
         // Rebuild the whole tree
         $table->rebuild();
      }
      return true;
   }
}


Top
 Profile  
 
PostPosted: Fri May 04, 2012 2:48 pm 
Joomla! Apprentice
Joomla! Apprentice

Joined: Tue Jun 16, 2009 5:11 pm
Posts: 24
This is great, just what I've been looking for. But if you want this code to actually work you'll need to add another line to getExtensionIds():

Code:
   protected function getExtensionIds($component) {
      $db = JFactory::getDbo();
      $query = $db->getQuery(true);
      $query->select('extension_id');
      $query->from('#__extensions');
      $cleanComponent = filter_var($component, FILTER_SANITIZE_MAGIC_QUOTES);
      $cleanComponent = strtolower(preg_replace('/\s/', '', $cleanComponent));
      $query->where($query->qn('name') . ' = ' . $query->quote($cleanComponent));
      $db->setQuery($query);
      $ids = $db->loadResultArray();
      return $ids;
   }


MattLG


Top
 Profile  
 
PostPosted: Fri May 04, 2012 3:56 pm 
Joomla! Explorer
Joomla! Explorer

Joined: Fri Nov 11, 2011 9:43 pm
Posts: 432
Location: Chicago, IL
You assign menu items for the components drop down menu in 2.5 using your component manifest file. There should be a node in the administrator section called <menu></menu> which might also have some <submenu></submenu> entries.


Top
 Profile  
 
PostPosted: Fri May 04, 2012 4:52 pm 
Joomla! Enthusiast
Joomla! Enthusiast

Joined: Thu Oct 30, 2008 7:27 pm
Posts: 149
Thanks for your correct/addition MattLG!


bbolli wrote:
You assign menu items for the components drop down menu in 2.5 using your component manifest file. There should be a node in the administrator section called <menu></menu> which might also have some <submenu></submenu> entries.


You are correct, but the subject of this thread is not how to create a menu item, but how to prevent the creation of a menu item. The <menu></menu> element of the manifest file does not provide that functionality.


Top
 Profile  
 
PostPosted: Fri May 04, 2012 5:00 pm 
Joomla! Explorer
Joomla! Explorer

Joined: Fri Nov 11, 2011 9:43 pm
Posts: 432
Location: Chicago, IL
Quote:
You are correct, but the subject of this thread is not how to create a menu item, but how to prevent the creation of a menu item. The <menu></menu> element of the manifest file does not provide that functionality.


I missed the part you wanted to programmatic-ally control that, my assumption was you wanted to disable the menu item permanently, which can very easily be done by removing the <menu></menu> element from the manifest, which would the KISS approach if you didn't need to change the view state of the menu item within your component.

Glad you got the issue resolved regardless.


Top
 Profile  
 
PostPosted: Fri May 04, 2012 8:03 pm 
Joomla! Apprentice
Joomla! Apprentice

Joined: Tue Jun 16, 2009 5:11 pm
Posts: 24
bbolli wrote:
which can very easily be done by removing the <menu></menu> element from the manifest


As I think has already been mentioned, that is incorrect. The only way is to do it programatically, such as with the above script.


Top
 Profile  
 
PostPosted: Fri May 04, 2012 10:43 pm 
Joomla! Explorer
Joomla! Explorer

Joined: Fri Nov 11, 2011 9:43 pm
Posts: 432
Location: Chicago, IL
Yeah, I get it; and that was in fact what I said in my last post; that I initially misread the question and my solution would only work:

Quote:
if you didn't need to change the view state of the menu item within your component.


Again, glad you got your problem solved.


Top
 Profile  
 
PostPosted: Fri Aug 10, 2012 8:10 pm 
Joomla! Apprentice
Joomla! Apprentice

Joined: Wed Dec 23, 2009 8:56 pm
Posts: 12
Fantastic bit of code, drewgg. My thanks for it. Worked just as I needed it to with almost no difficulty.

Again thanks for the code!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 12 posts ] 



Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group