Hi folks
My scenario: I create an article, and using afterstore I create a single article menu item to point to it (good for nice urls).
I have this working nicely, but I wanted to alter the menu item's title, alias, and meta description if I ever changed the articles title.
// Get article Title. $artTitle = $fields['art_title']->value; // value could be 'Hey There' // Create an Alias, $artAlias = JFilterOutput::stringURLSafe($artTitle); // value would become 'hey-there' $artId = $fields['art_id']->value; // Find correct menu item based on menu link $artMenuLink = 'index.php?option=com_content&view=article&id=' . $artId; // get Menu Info $dbMenu = JFactory::getDbo(); $queryMenu = $dbMenu->getQuery(true); $queryMenu ->select(array('id', 'title', 'params')) ->from($dbMenu->quoteName('#__menu')) ->where($dbMenu->quoteName('link') . ' LIKE '. $dbMenu->quote($artMenuLink)); $dbMenu->setQuery($queryMenu); $menuResult = $dbMenu->loadObject(); $menuId = $menuResult->id; $menuTitle = $menuResult->title; $menuParams = $menuResult->params; // turn the $menuParams in to an array $menuParamsArray = json_decode($menuParams, true); // true makes it an array, rather than an object // if menu title is different to article title if ($menuTitle != $artTitle) { // update with the new title $menuTitle = $artTitle; $menuAlias = $artAlias; $menuMetaDesc = 'Fretroom Lesson File: '.$artTitle; // update relevant part of array $menuParamsArray['menu-meta_description'] = $menuMetaDesc; $menuParamsString = ''; $menuParamsArrayLength = count($menuParamsArray); // create the string to be stored in params foreach($menuParamsArray as $menuk => $menuv) { for($i=1;$i<$menuParamsArrayLength;$i++) { $menuParamsString .= '"'.$menuk.'":"'.$menuv.'",'; } // The last entry must NOT have a trailing ',' for($i=1;$i==$menuParamsArrayLength;$i++) { $menuParamsString .= '"'.$menuk.'":"'.$menuv.'"'; } } $menuParams = '{'.$menuParamsString.'}'; $queryMenu2 = $dbMenu->getQuery(true); // Fields to update. $menuFields = array( $dbMenu->quoteName('title') . ' = ' . $dbMenu->quote($artTitle), $dbMenu->quoteName('alias') . ' = ' . $dbMenu->quote($artTitle), $dbMenu->quoteName('params') . ' = ' . $dbMenu->quote($menuParams) ); // Conditions for which records should be updated. $menuConditions = array( $dbMenu->quoteName('id') . ' = ' . $dbMenu->quote($menuId) ); $queryMenu2->update($dbMenu->quoteName('#__menu'))->set($menuFields)->where($menuConditions); $dbMenu->setQuery($queryMenu2); $menuComplete = $dbMenu->execute(); }