2

This question was raised on the Joomla General Development Google group list but I was unable to respond successfully (6 replies deleted - I obviously did something wrong) - so I thought I'd raise the questions here and answer them.

Q1 - In my install script I like to report to the user in a message what version of the component has just been installed/uninstalled. In J3 I read the version from the manifest file as the best source of truth thus:

$message = 'Installing xbOSM component v.' . $parent->get('manifest')->version . ' ' . $parent->get('manifest')->creationDate;

Factory::getApplication()->enqueueMessage($message, 'Info');

Q2 - with some components when installing I need to create a default category ('Uncategorised') for the component in the install script. Usually I create SQL insert, but is there another way that suits both J3 & J4?

1
  • Thanks for the corrections Mick. Cheers Jul 3 at 6:42
3

Q1 - In J4 I found that this simple function gets what you might want:

/**
 * Get the version of the component
 * @return version element of manifest
 */
public static function getComponentVersion()
{
    $componentXML = Installer::parseXMLInstallFile(Path::clean(JPATH_ADMINISTRATOR . '/components/com_mycomponent/mycomponent.xml'));

    return $componentXML['version'];
}

Q2 - I use a separate function to create categories based on a passed array:

/**
 * The extension name. This should be set in the installer script.
 */
protected $extension = 'com_mycomponent';

/**
 * method to run after an install/update/uninstall method
 * @return void
 */
public function postflight($type, $parent)
{
    // $parent is the class calling this method
    // $type is the type of change (install, update or discover_install)
    // the variable $type was returning lowercase values, it must be uppercase
    $type = strtoupper($type);
    echo '<p>' . Text::_('COM_MYCOMONENT_POSTFLIGHT_' . $type . '_TEXT') . '</p>';

    // setup data for first time install
    if ($type == "INSTALL") {
        
        // Load categories
        $cattitles = array(
            "2020"
        );
        $cat_id = $this->createCategory($cattitles);
    }

    // Clear Joomla system cache.
    $cache = Factory::getCache();
    $cache->clean('_system');
}


/**
* Function to create category records
* @param array category titles
* @param string category group or type
* @return void
*/
public function createCategory($cat_titles)
{
    foreach ($cat_titles as $cat) {
        $category = Table::getInstance('Category');
        $category->extension = $this->extension;
        $category->title = $cat;
        $category->description = '';
        $category->published = 1;
        $category->access = 1;
        $category->params = '{"category_layout":"","image":"","image_alt":""}';
        $category->metadata = '{"page_title":"","author":"","robots":""}';
        $category->language = '*';

        // Set the location in the tree
        $category->setLocation(1, 'last-child');

        // Check to make sure our data is valid
        if (!$category->check()) {
            throw new Exception(500, $category->getError());
            return false;
        }

        // Now store the category
        if (!$category->store(true)) {
            throw new Exception(500, $category->getError());
            return false;
        }
    }

    // Build the path for our category
    $category->rebuildPath($category->id);
    echo '<p>' . Text::_('Categories created') . '</p>';
    return $category->id;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.