Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add user extension as horizontal extension #110

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions extensions/Article/Command/DisplayCommandHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

namespace Joomla\Extension\Article\Command;

use Joomla\Content\Type\Attribution;
use Joomla\Content\Type\Compound;
use Joomla\Content\Type\Headline;
use Joomla\Content\Type\Paragraph;
Expand All @@ -34,7 +33,6 @@ protected function getElements(EntityInterface $entity)
'section',
[
new Headline($child->title, 2),
$child->author != $article->author ? new Attribution('Contribution from', $child->author) : null,
new Paragraph($child->body),
]
);
Expand Down
7 changes: 0 additions & 7 deletions extensions/Article/Entity/Article.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,6 @@
description="COM_CONTENT_FIELD_ARTICLETEXT_DESC"
/>

<field name="author"
type="text"
label="COM_CONTENT_FIELD_CREATED_BY_ALIAS_LABEL"
description="COM_CONTENT_FIELD_CREATED_BY_ALIAS_DESC"
default=""
/>

<field name="license"
type="text"
label="JFIELD_META_RIGHTS_LABEL"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ class AddCategoryContentTypesHandler extends QueryHandler

public function handle (ContentTypeQuery $query)
{
if (!isset($query->entity->category))
{
return $query->elements;
}

$category = $query->entity->category;

$elements = $query->elements;
Expand Down
10 changes: 8 additions & 2 deletions extensions/Category/Listener/AddRelationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
/**
* Class AddRelationListener
*
* @package Joomla\Extension\Content
* @package Joomla\Extension\Category
*
* @since 1.0
*/
Expand All @@ -33,10 +33,16 @@ class AddRelationListener
*/
public function addCategoryRelation(AfterCreateDefinitionEvent $event)
{
// @todo belongs to the relation table as well
if ($event->getArgument('entityName') != 'Article')
{
return ;
}

/** @var \Joomla\ORM\Entity\EntityBuilder $builder */
$builder = $event->getArgument('builder');

// @todo the relation needs to be looked up here trough a relation table */
// @todo the relation needs to be looked up here trough a relation table
$id = 1;

$relation = new HasOne(['id' => 1, 'name' => 'category_id', 'type' => 'hasOne', 'entity' => 'Category', 'reference' => 'id']);
Expand Down
38 changes: 38 additions & 0 deletions extensions/User/Command/AddAuthorContentTypesHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* Part of the Joomla! User Extension Package
*
* @copyright Copyright (C) 2015 - 2016 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
namespace Joomla\Extension\User\Command;
use Joomla\Service\QueryHandler;
use Joomla\Cms\Service\ContentTypeQuery;
use Joomla\Content\Type\Attribution;
use Joomla\Session\SessionAwareTrait;

/**
* User Command Handler
*
* @package Joomla/Extension/User
*
* @since 1.0
*/
class AddAuthorContentTypesHandler extends QueryHandler
{
use SessionAwareTrait;

public function handle (ContentTypeQuery $query)
{
if (!isset($query->entity->author))
{
return $query->elements;
}

$author = $query->entity->author;

$elements = $query->elements;
$elements['author'] = new Attribution('Contribution from ', $author->name);
return $elements;
}
}
36 changes: 36 additions & 0 deletions extensions/User/Command/AddLoggedInUserContentTypesHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* Part of the Joomla! User Extension Package
*
* @copyright Copyright (C) 2015 - 2016 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
namespace Joomla\Extension\User\Command;

use Joomla\Service\QueryHandler;
use Joomla\Cms\Service\ContentTypeQuery;
use Joomla\Content\Type\Attribution;
use Joomla\Session\SessionAwareTrait;

/**
* User Command Handler
*
* @package Joomla/Extension/User
*
* @since 1.0
*/
class AddLoggedInUserContentTypesHandler extends QueryHandler
{
use SessionAwareTrait;

public function handle(ContentTypeQuery $query)
{
$user = $this->getSession()->get('User');

$elements = [
'user' => new Attribution('Logged in user ', $user ? $user->name : 'Anonymous')
];
$elements += $query->elements;
return $elements;
}
}
22 changes: 22 additions & 0 deletions extensions/User/Command/DisplayCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* Part of the Joomla! User Extension Package
*
* @copyright Copyright (C) 2015 - 2016 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/

namespace Joomla\Extension\User\Command;

use Joomla\Cms\Service\BasicDisplayCommand;

/**
* Display Command
*
* @package Joomla/Extension/User
*
* @since 1.0
*/
class DisplayCommand extends BasicDisplayCommand
{
}
34 changes: 34 additions & 0 deletions extensions/User/Command/DisplayCommandHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* Part of the Joomla! User Extension Package
*
* @copyright Copyright (C) 2015 - 2016 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
namespace Joomla\Extension\User\Command;

use Joomla\Cms\Service\BasicDisplayCommandHandler;
use Joomla\ORM\Entity\EntityInterface;
use Joomla\Content\Type\Headline;
use Joomla\Content\Type\Paragraph;

/**
* Display Command Handler
*
* @package Joomla/Extension/User
*
* @since 1.0
*/
class DisplayCommandHandler extends BasicDisplayCommandHandler
{

protected function getElements(EntityInterface $entity)
{
$elements = parent::getElements($entity);

$elements['title'] = new Headline('Details of ' . $entity->name, 1);
$elements['body'] = new Paragraph('Username: ' . $entity->username);

return $elements;
}
}
37 changes: 37 additions & 0 deletions extensions/User/Entity/User.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE entity SYSTEM
"https://github.com/nibralab/joomla-architecture/blob/master/code/Joomla/ORM/Definition/entity.dtd">
<entity name="user">

<storage>
<default table="user"/>
</storage>

<fields>
<field name="id"
type="id"
label="JGLOBAL_FIELD_ID_LABEL"
description="JGLOBAL_FIELD_ID_DESC"
default="null"
>
<validation rule="positive"/>
<validation rule="integer"/>
</field>

<field name="username"
type="title"
label="JGLOBAL_TITLE"
>
</field>
<field name="name"
type="title"
label="JGLOBAL_TITLE"
>
</field>

</fields>

<relations>
</relations>

</entity>
52 changes: 52 additions & 0 deletions extensions/User/Listener/AddRelationListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* Part of the Joomla! User Extension Package
*
* @copyright Copyright (C) 2015 - 2016 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/

namespace Joomla\Extension\User\Listener;


use Joomla\ORM\Event\AfterCreateDefinitionEvent;
use Joomla\ORM\Definition\Parser\Relation;
use Joomla\ORM\Definition\Locator\Locator;
use Joomla\ORM\Definition\Locator\Strategy\RecursiveDirectoryStrategy;
use Joomla\ORM\Definition\Parser\HasOne;

/**
* Class AddRelationListener
*
* @package Joomla\Extension\User
*
* @since 1.0
*/
class AddRelationListener
{
/**
* Event handler
*
* @param AfterCreateDefinitionEvent $event The event
*
* @return void
*/
public function addAuthorRelation(AfterCreateDefinitionEvent $event)
{
// @todo belongs to the relation table as well
if ($event->getArgument('entityName') != 'Article')
{
return ;
}

/** @var \Joomla\ORM\Entity\EntityBuilder $builder */
$builder = $event->getArgument('builder');

// @todo the relation needs to be looked up here trough a relation table
$id = 1;

$relation = new HasOne(['id' => 1, 'name' => 'author_id', 'type' => 'hasOne', 'entity' => 'User', 'reference' => 'id']);

$builder->handleHasOne($relation, new Locator([new RecursiveDirectoryStrategy(__DIR__ . '/../Entity')]));
}
}
12 changes: 12 additions & 0 deletions extensions/User/config/extension.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
listeners:
AddRelationListener:
class: \Joomla\Extension\User\Listener\AddRelationListener
events:
onAfterCreateDefinition: addAuthorRelation
queryhandlers:
AddAuthorContentTypesHandler:
class: \Joomla\Extension\User\Command\AddAuthorContentTypesHandler
query: \Joomla\Cms\Service\ContentTypeQuery
AddLoggedInUserContentTypesHandler:
class: \Joomla\Extension\User\Command\AddLoggedInUserContentTypesHandler
query: \Joomla\Cms\Service\ContentTypeQuery
6 changes: 6 additions & 0 deletions installation/demo.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@
'body' => 'Demo category body',
'parent_id' => 0
]
],
'User' => [
[
'username' => 'demo',
'name' => 'Demo User'
]
]
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

namespace Joomla\Cms\Service;

use Joomla\Content\Type\Attribution;
use Joomla\Content\Type\Compound;
use Joomla\Content\Type\Headline;
use Joomla\Content\Type\Paragraph;
Expand Down Expand Up @@ -65,10 +64,6 @@ protected function getElements(EntityInterface $entity)
{
$elements['title'] = new Headline($entity->title, 1);
}
if ($entity->has('author'))
{
$elements['author'] = new Attribution('Written by', $entity->author);
}
if ($entity->has('teaser'))
{
$elements['teaser'] = new Paragraph($entity->teaser, Paragraph::EMPHASISED);
Expand Down
4 changes: 2 additions & 2 deletions libraries/incubator/Content/Type/Attribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ class Attribution extends AbstractContentType
/**
* Attribution constructor.
*
* @param string $label The text before the author's name
* @param string $name The author's name
* @param string $label The label
* @param string $name The name
*/
public function __construct($label, $name)
{
Expand Down
7 changes: 6 additions & 1 deletion libraries/incubator/Extension/FileExtensionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,12 @@ private function createQueryHandlers(Extension $extension, array $handlersConfig
{
foreach ($handlersConfig as $handler)
{
$extension->addQueryHandler($handler['query'], new $handler['class']($this->container->get('CommandBus'), $this->container->get('EventDispatcher')));
$handlerInstance = new $handler['class']($this->container->get('CommandBus'), $this->container->get('EventDispatcher'));
if (method_exists($handlerInstance, 'setSession') && $this->container->has('Session'))
{
$handlerInstance->setSession($this->container->get('Session'));
}
$extension->addQueryHandler($handler['query'], $handlerInstance);
}
}
}
Loading