Skip to content

Commit

Permalink
Added changes introduced by alpha-6 for D7: https://www.drupal.org/no…
Browse files Browse the repository at this point in the history
  • Loading branch information
mccrodp committed Apr 3, 2016
1 parent 1b386e7 commit 2f066f9
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 2 deletions.
3 changes: 3 additions & 0 deletions message_private.links.task.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ message_private.messages:
route_name: message_private.messages
base_route: entity.user.canonical
title: 'Messages'
# Provide dynamic local tasks.
message_private.messages:
deriver: 'Drupal\message_private\Plugin\Derivative\MessagePrivateLocalTasks'
weight: -10
2 changes: 1 addition & 1 deletion message_private.module
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ function template_preprocess_message_private(&$variables) {
// @see https://www.drupal.org/node/2195739
// $variables['name'] = theme('username', array('account' => user_load($uid)));

$variables['submitted'] = t('Submitted by !username on !datetime', array(
$variables['submitted'] = t('Sent by !username on !datetime', array(
'!username' => $variables['name'],
'!datetime' => $variables['date'],
));
Expand Down
9 changes: 8 additions & 1 deletion src/Form/MessagePrivateSettingsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,15 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#description' => t('Global On / Off checkbox for emails notifying users of a new private message'),
);

// Local action links on/off checkbox.
$form[MESSAGE_PRIVATE_LOCAL_ACTION] = array(
'#type' => 'checkbox',
'#title' => t('Disable Create a New Message local action links'),
'#default_value' => variable_get(MESSAGE_PRIVATE_LOCAL_ACTION, FALSE),
'#description' => t('Disable local action links to create new message on user pages.'),
);

// Role based message create limit on/off checkbox.
// Role based message create limit on/off checkbox.
$form[MESSAGE_PRIVATE_MESSAGE_LIMIT] = array(
'#type' => 'checkbox',
'#title' => t('Limit Message Create By Role'),
Expand Down
31 changes: 31 additions & 0 deletions src/Plugin/Derivative/MessagePrivateLocalTasks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* @file
* Contains \Drupal\message_private\Plugin\Derivative\MessagePrivateLocalTasks.
*/

namespace Drupal\message_private\Plugin\Derivative;

use Drupal\Component\Plugin\Derivative\DeriverBase;

/**
* Defines dynamic local tasks.
*/
class DynamicLocalTasks extends DeriverBase {

/**
* {@inheritdoc}
*/
public function getDerivativeDefinitions($base_plugin_definition) {
// Create the "Create message for___" tasks.
// @todo set no task for 'user/%' 'user/%/messages' where variable_get('message_private_local_action_links', FALSE)
// @todo: to be set only for users with private message permissions.
$this->derivatives['message_private.messages'] = $base_plugin_definition;
$this->derivatives['message_private.messages']['title'] = 'Messages';
// @todo: this should pass a user / uid argument also.
$this->derivatives['message_private.messages']['route_name'] = 'message_private.user.create_message';
return $this->derivatives;
}
}

?>
158 changes: 158 additions & 0 deletions src/Plugin/views/access/MessagePrivateInbox.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<?php

/**
* @file
* Contains \Drupal\message_private\Plugin\views\access\MessagePrivateInbox.
*/

namespace Drupal\message_private\Plugin\views\access;

use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\user\PermissionHandlerInterface;
use Drupal\views\Plugin\views\access\AccessPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Routing\Route;

/**
* Access plugin that provides user and permission-based access control.
*
* @ingroup views_access_plugins
*
* @ViewsAccess(
* id = "perm",
* title = @Translation("Message Private Inbox"),
* help = @Translation("Access will be granted for user's own inbox / users with bypass permissions.")
* )
*/
// @todo: rework access plugin to that of D7: http://cgit.drupalcode.org/message_private/tree/message_private_access_plugin.inc
class Permission extends AccessPluginBase implements CacheableDependencyInterface {

/**
* {@inheritdoc}
*/
protected $usesOptions = TRUE;

/**
* The permission handler.
*
* @var \Drupal\user\PermissionHandlerInterface
*/
protected $permissionHandler;

/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;

/**
* Constructs a Permission object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\user\PermissionHandlerInterface $permission_handler
* The permission handler.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, PermissionHandlerInterface $permission_handler, ModuleHandlerInterface $module_handler) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->permissionHandler = $permission_handler;
$this->moduleHandler = $module_handler;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('user.permissions'),
$container->get('module_handler')
);
}

/**
* {@inheritdoc}
*/
public function access(AccountInterface $account) {
return $account->hasPermission($this->options['perm']);
}

/**
* {@inheritdoc}
*/
public function alterRouteDefinition(Route $route) {
$route->setRequirement('_permission', $this->options['perm']);
}

public function summaryTitle() {
$permissions = $this->permissionHandler->getPermissions();
if (isset($permissions[$this->options['perm']])) {
return $permissions[$this->options['perm']]['title'];
}

return $this->t($this->options['perm']);
}


protected function defineOptions() {
$options = parent::defineOptions();
$options['perm'] = array('default' => 'access content');

return $options;
}

public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
// Get list of permissions
$perms = [];
$permissions = $this->permissionHandler->getPermissions();
foreach ($permissions as $perm => $perm_item) {
$provider = $perm_item['provider'];
$display_name = $this->moduleHandler->getName($provider);
$perms[$display_name][$perm] = strip_tags($perm_item['title']);
}

$form['perm'] = array(
'#type' => 'select',
'#options' => $perms,
'#title' => $this->t('Permission'),
'#default_value' => $this->options['perm'],
'#description' => $this->t('Only users with the selected permission flag will be able to access this display.'),
);
}

/**
* {@inheritdoc}
*/
public function getCacheMaxAge() {
return Cache::PERMANENT;
}

/**
* {@inheritdoc}
*/
public function getCacheContexts() {
return ['user.permissions'];
}

/**
* {@inheritdoc}
*/
public function getCacheTags() {
return [];
}

}

0 comments on commit 2f066f9

Please sign in to comment.