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

Refactor membership module to better support plugins. Update namespac… #15

Open
wants to merge 3 commits into
base: 8.x-1.x
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions membership.permissions.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
add membership entities:
title: 'Create new Membership entities'

create membership for other user:
title: 'Create a membership for another user'

administer membership entities:
title: 'Administer Membership entities'
description: 'Allow to access the administration form to configure Membership entities.'
Expand Down
6 changes: 6 additions & 0 deletions membership.workflows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@ membership_default:
label: Default Membership Workflow
group: membership
states:
pending:
label: Pending
active:
label: Active
expired:
label: Expired
transitions:
activate:
label: Activate
from: [pending]
to: active
expire:
label: Expire
from: [active]
Expand Down

This file was deleted.

This file was deleted.

2 changes: 1 addition & 1 deletion modules/membership_term/src/Entity/MembershipTerm.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\membership\Entity\MembershipTermInterface;
use Drupal\membership\Entity\MembershipInterface;
use Drupal\membership\EventDispatcherTrait;
use Drupal\user\UserInterface;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,39 @@ interface MembershipTermInterface extends ContentEntityInterface, EntityChanged

// Add get/set methods for your configuration properties here.


/**
* Gets the Membership term type.
* Gets the membership for a term
*
* @return string
* The Membership term type.
* @return Membership
* The membership entity
*/
public function getType();
public function getMembership();

/**
* Gets the Membership term name.
* Set a membership on a term.
*
* @return string
* Name of the Membership term.
* @param \Drupal\membership\Entity\MembershipInterface $membership
*
* @return \Drupal\membership\Entity\MembershipTermInterface
* The called Membership term.
*/
public function getName();

public function setMembership(MembershipInterface $membership);
/**
* Sets the Membership term name.
* Gets the membership id for a term
*
* @param string $name
* The Membership term name.
* @return integer
* The membership entity_id
*/
public function getMembershipId();

/**
* Gets the Membership term type.
*
* @return \Drupal\membership\Entity\MembershipTermInterface
* The called Membership term entity.
* @return string
* The Membership term type.
*/
public function setName($name);
public function getType();

/**
* Gets the Membership term creation timestamp.
Expand All @@ -61,26 +68,6 @@ public function getCreatedTime();
*/
public function setCreatedTime($timestamp);

/**
* Returns the Membership term published status indicator.
*
* Unpublished Membership term are only visible to restricted users.
*
* @return bool
* TRUE if the Membership term is published.
*/
public function isPublished();

/**
* Sets the published status of a Membership term.
*
* @param bool $published
* TRUE to set this Membership term to published, FALSE to set it to unpublished.
*
* @return \Drupal\membership\Entity\MembershipTermInterface
* The called Membership term entity.
*/
public function setPublished($published);

/**
* Returns the Membership Type (bundle) that this membership term is associated with.
Expand Down
1 change: 0 additions & 1 deletion modules/membership_term/src/Entity/MembershipTermType.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Drupal\membership_term\Entity;

use Drupal\Core\Config\Entity\ConfigEntityBundleBase;
use Drupal\membership\Entity\MembershipTermTypeInterface;

/**
* Defines the Membership term type entity.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Drupal\membership\Entity;
namespace Drupal\membership_term\Entity;

use Drupal\Core\Config\Entity\ConfigEntityInterface;

Expand All @@ -27,6 +27,15 @@ public function setWorkflowId($workflow_id);
*/
public function getMembershipType();

/**
* @return string
*/
public function getTermLength();

/**
* @return string
*/
public function getGracePeriod();
/**
* @param string $membership_type
* @return string
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Drupal\membership\Entity;
namespace Drupal\membership_term\Entity;

use Drupal\views\EntityViewsData;

Expand Down
63 changes: 41 additions & 22 deletions src/Entity/Membership.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
use Drupal\entity\Revision\RevisionableContentEntityBase;
use Drupal\membership\EventDispatcherTrait;
use Drupal\membership\Exception\MembershipFeatureNotImplementedException;
use Drupal\membership\MembershipEvent;
use Drupal\membership\MembershipEvents;
use Drupal\membership\Event\MembershipEvent;
use Drupal\membership\Event\MembershipEvents;
use Drupal\user\UserInterface;

/**
Expand Down Expand Up @@ -46,8 +46,6 @@
* },
* base_table = "membership",
* revision_table = "membership_revision",
* data_table = "membership_field_data",
* revision_data_table = "membership_field_revision",
* admin_permission = "administer membership entities",
* field_ui_base_route = "entity.membership_type.edit_form",
* entity_keys = {
Expand Down Expand Up @@ -80,8 +78,13 @@ class Membership extends RevisionableContentEntityBase implements MembershipInte
public static function preCreate(EntityStorageInterface $storage_controller, array &$values) {
parent::preCreate($storage_controller, $values);
if (empty($values['user_id'])) {
$values['user_id'] = \Drupal::currentUser()->id();

$user = \Drupal::currentUser();
if ($user->hasPermission('create membership for other user')) {
return ;
}
else {
$values['user_id'] = \Drupal::currentUser()->id();
}
}
}

Expand Down Expand Up @@ -148,13 +151,14 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
->setDescription(t('The Membership type/bundle.'))
->setSetting('target_type', 'membership_type')
->setRequired(TRUE);

$fields['user_id'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Authored by'))
->setDescription(t('The owner of the Membership entity.'))
->setLabel(t('User'))
->setDescription(t('The owner of the Membership.'))
->setRevisionable(TRUE)
->setSetting('target_type', 'user')
->setSetting('handler', 'default')
->setDefaultValueCallback('Drupal\node\Entity\Node::getCurrentUserId')
->setDefaultValueCallback('Drupal\membership\Entity\Membership::getMembershipUserId')
->setTranslatable(TRUE)
->setDisplayOptions('view', array(
'label' => 'hidden',
Expand All @@ -173,6 +177,7 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
))
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);

$fields['data'] = BaseFieldDefinition::create('map')
->setLabel(t('Data'))
->setReadOnly(TRUE)
Expand Down Expand Up @@ -200,6 +205,7 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
->setDisplayConfigurable('view', TRUE)
->setRevisionable(TRUE)
->setSetting('workflow_callback', ['\Drupal\membership\Entity\Membership', 'getWorkflowId']);

$fields['provider'] = BaseFieldDefinition::create('membership_provider_id')
->setLabel('Provider plugin/remote ID')
->setDisplayConfigurable('form', false)
Expand All @@ -209,24 +215,28 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
}

/**
* @inheritDoc
* Default value callback for 'Membership::user_id' base field definition.
*
* @see ::baseFieldDefinitions()
*
* @return array
* An array of default values.
* @return
*/
public function getTerm() {
return $this->get('membership_term')->entity;
public static function getMembershipUserId() {
$user = \Drupal::currentUser();
if ($user->hasPermission('create membership for other user')) {
return [];
}
else {
return [$user->id()];
}
}

/**
* @inheritDoc
*/
public function preSave(EntityStorageInterface $storage) {
if (!$this->isNew() && ($storage->loadUnchanged($this->id())->state->getValue()) != $this->state->getValue()) {
$event = new MembershipEvent($this);
$this->getEventDispatcher()->dispatch(MembershipEvents::STATE_CHANGE, $event);
if ($this->isExpired()) {
$this->getEventDispatcher()->dispatch(MembershipEvents::EXPIRE, $event);
}
}
parent::preSave($storage);
public function getTerm() {
return $this->get('membership_term')->entity;
}

/**
Expand All @@ -248,6 +258,15 @@ public function postCreate(EntityStorageInterface $storage) {
parent::postCreate($storage);
}

/**
* @inheritDoc
*/
function postSave(EntityStorageInterface $storage, $update = TRUE) {
$event = new MembershipEvent($this);
$this->getEventDispatcher()->dispatch(MembershipEvents::UPDATED, $event);
parent::postSave($storage, $update);

}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need to throw our own event here? State Machine module emits an event on state changes, and core already has hook_entity_insert() and friends, that while not a Symfony event provides similar functionality. Not opposed to an event per se but if it basically duplicates the hook that might not be worth the extra overhead here.

In the same vein I think we can back out the STATE_CHANGE and EXPIRE invocations in preSave() given that state machine does the workflow transition event. That was a late addition in state machine so I'm thinking that code predates those events being thrown for us.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Still haven't gotten to the state change events yet -- probably will be in there today or tomorrow -- so I haven't seen how state_machine handles this.

I was trying to avoid hook_entity_insert() and hook_entity_update() and go with a more OO approach, and this looked like the best way...

What's your sense of preference going forward -- symfony events vs hooks? I would prefer events, just because that's a pattern I use on a bunch of projects (and never particularly liked hooks). But I can live with a hook here if they aren't going to be deprecated... I was surprised to not find any decorator or observers related to the Entity Manager to supersede these hooks...


/**
* @inheritDoc
Expand Down
1 change: 1 addition & 0 deletions src/Entity/MembershipType.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public function setWorkflowId($workflow_id) {
return $this;
}


/**
* @inheritDoc
*
Expand Down
3 changes: 2 additions & 1 deletion src/Entity/MembershipTypeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Drupal\membership\Entity;

use Drupal\entity\Entity\RevisionableEntityBundleInterface;
use Drupal\membership\Plugin\MembershipProviderInterface;

/**
* Provides an interface for defining Membership type entities.
Expand All @@ -24,7 +25,7 @@ public function getWorkflowId();

/**
* @param string $workflow_id
* @return mixed
* @return MembershipTypeInterface This object
*/
public function setWorkflowId($workflow_id);

Expand Down
2 changes: 1 addition & 1 deletion src/MembershipEvent.php → src/Event/MembershipEvent.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Drupal\membership;
namespace Drupal\membership\Event;

use Drupal\membership\Entity\MembershipInterface;
use Symfony\Component\EventDispatcher\Event;
Expand Down
6 changes: 5 additions & 1 deletion src/MembershipEvents.php → src/Event/MembershipEvents.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Drupal\membership;
namespace Drupal\membership\Event;

/**
* Class MembershipEvents
Expand All @@ -21,6 +21,10 @@ final class MembershipEvents {
*/
const CREATED = 'membership.create';

/**
* Membership is updated.
*/
const UPDATED = 'membership.updated';
/**
* Membership state changes.
*/
Expand Down
Loading