Skip to content

Version 1.4.1 - Stable

Compare
Choose a tag to compare
@phproberto phproberto released this 30 Aug 11:55

How to install / upgrade

Download joomla-entity-v1.4.1.zip an install through Joomla! Extension Manager.

New Features

New Module entity

use Phproberto\Joomla\Entity\Core\Module;

$module = Module::find(1);

// Do something if module is published
if ($module->isPublished())
{
    echo 'Module ' . $module->get('title') . ' is published!';
}

// Module entity implements HasParams trait so you can easily access params like this
if ('mainmenu' === $module->param('menutype'))
{
	echo 'Module ' . $module->get('title') . ' is publishing Main Menu';
}

// Module entity implements HasClient
if ($module->client()->isSite())
{
	echo 'Module ' . $module->get('title') . ' is a frontend module';
}

// Retrieve the menu items where the module is available
echo 'Module ' . $module->get('title') . ' is published in menu items: ' . implode(',', $module->menusIds());

New HasLevel trait

This trait is designed to access level columns in entities like categories or other nested entites but you can use it for any entity with a column storing a numeric value you need to compare against other entities.

use Phproberto\Joomla\Entity\Content\Category;

$category = Category::find(23);

if ($category->hasLevel(0))
{
	echo 'Category ' . $category->get('title') . ' is a root category.';
}
else
{
	echo 'Category ' . $category->get('title') . ' is a level ' . $category->level() . ' category.';
}

if ($category->hasLevelBetween(1,5))
{
	echo 'Category ' . $category->get('title') . ' has a level between 1 and 5';
}

if ($category->hasLevelGreater(1))
{
	echo 'Category ' . $category->get('title') . ' has a level greater than 1';
}

if ($category->hasLevelLower(6))
{
	echo 'Category ' . $category->get('title') . ' has a level lower than 6';
}

New HasParent trait.

This trait is designed to access parent entities in nested entities but you can use it in any entity where you store a column pointing to a parent entity.

use Phproberto\Joomla\Entity\Content\Category;

$category = Category::find(23);

echo 'Category ' . $category->parent()->get('title') . ' is the parent category of ' . $category->get('title');

// You can access parent id without loading it through parentId()
$extensions = Category::find(20);

if ($extensions->id() === $category->parentId())
{
	echo 'Extensions is the parent category of ' . $category->get('title');
}

New HasAncestors trait.

This trait is designed for entities using a nested structure like categories.

use Phproberto\Joomla\Entity\Content\Category;

$category = Category::find(23);

// Show the tree of ancestors for a category
foreach ($category->ancestors() as $ancestor)
{
	// Avoid showing root level
	if ($ancestor->hasLevel(0))
	{
		continue;
	}

	echo '<pre>';
	echo str_repeat('--', $ancestor->level() - 1) . ' ' . $ancestor->id() . '.' . $ancestor->get('title');
	echo '</pre>';
}

It will show something like:

 14.Sample Data-Articles
-- 19.Joomla!
---- 20.Extensions

Mixing it with the power of collections you can do cool things:

// Check if a category is an ancestor
if ($category->ancestors()->has(20))
{
	echo '<pre>Category ' . Category::find(20)->get('title') . ' is an ancestor of ' .$category->get('title') . '</pre>';
}

You can also search within ancestors!

// Searching ancestors without root category
echo '<h3>Not root ancestors</h3>';
foreach ($category->searchAncestors(['filter.ancestor_id' => 1]) as $ancestor)
{
	echo '<pre>Category ' . $ancestor->get('title') . ' is an ancestor of ' . $category->get('title') . '</pre>';
}

// You can also apply custom ordering
echo '<h3>Not root ancestors ordered by title</h3>';
foreach ($category->searchAncestors(['filter.ancestor_id' => 1, 'list.ordering' => 'c.title']) as $ancestor)
{
	echo '<pre>Category ' . $ancestor->get('title') . ' is an ancestor of ' . $category->get('title') . '</pre>';
}

// And custom ordering in custom direction
echo '<h3>Not root ancestors ordered by title descendently</h3>';

foreach ($category->searchAncestors(['filter.ancestor_id' => 1, 'list.ordering' => 'c.title', 'list.direction' => 'DESC']) as $ancestor)
{
	echo '<pre>Category ' . $ancestor->get('title') . ' is an ancestor of ' . $category->get('title') . '</pre>';
}

New HasDescendants trait.

This trait is designed for entities using a nested structure like categories.

use Phproberto\Joomla\Entity\Content\Category;

$category = Category::find(23);

echo '<h3>Descendant categories of ' . $category->get('title') . '</h3>';

foreach ($category->descendants() as $descendant)
{
	echo '<pre>';
	echo str_repeat('--', $descendant->level() - 1) . ' ' . $descendant->id() . '.' . $descendant->get('title');
	echo '</pre>';
}

It will show something like:

Descendant categories of Templates

-------- 69.Beez 20
-------- 70.Beez 5
-------- 68.Atomic

You can also use searchDescendants() to perform custom filtering like explained in the HasAncestors trait info.

Introducing searchers

This release starts to integrate the concept of searcher which represents a class to find entities of X type. For example the CategorySearcher searches data to feed entities of type Category.

use Phproberto\Joomla\Entity\Categories\CategorySearcher;

// Search all the categories with access level equal to 2
$categories = CategorySearcher::instance(['filter.access' => 2, 'list.limit' => 0])->search();

// Search all the unpublished categories
$categories = CategorySearcher::instance(['filter.published' => 0, 'list.limit' => 0])->search();

// Search all the categories with a specific ancestor
$categories = CategorySearcher::instance(['filter.ancestor_id' => 20, 'list.limit' => 0])->search();

// Search all the categories with a specific descendant
$categories = CategorySearcher::instance(['filter.descendant_id' => 21, 'list.limit' => 0])->search();

There are a lot of things to add to the CategorySearcher like filtering by level, language, search by title, etc. Feel free to contribute it or just wait until it's needed by someone else and contributed.

New Collection::fromData() method.

This method is a fast way to generate a collection from an array of data.

use Phproberto\Joomla\Entity\Collection;
use Phproberto\Joomla\Entity\Categories\CategorySearcher;
use Phproberto\Joomla\Entity\Content\Category;

$unpublishedCategories = Collection::fromData(
    // Data
    CategorySearcher::instance(['filter.published' => 0])->search(), 
    // Associated entity
    Category::class
);

// Having a collection gives you some benefits over pure data because you have entities there and method to retrieve ids, etc.
echo 'Unpublished categories with ids: ' . implode(',', $unpublishedCategories->ids());

foreach ($unpublishedCategories as $category)
{
    echo 'Category ' . $category->get('title') . ' is unpublished';
}