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;
}