Skip to content

Commit 0bd0e6e

Browse files
committed
Merge branch '7.x-1.x' of github.com:Gizra/message into 7.x-1.x
2 parents 338e7af + 777a915 commit 0bd0e6e

19 files changed

+830
-539
lines changed

.travis.yml

+11-8
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ php:
44
- 5.3
55

66
mysql:
7-
database: drupal
7+
database: message
88
username: root
99
encoding: utf8
1010

1111
before_script:
12-
# navigate out of module directory to prevent blown stack by recursive module lookup
12+
# navigate out of module directory to prevent blown stack by recursive module
13+
# lookup
1314
- cd ../..
1415

1516
# install drush
@@ -21,16 +22,18 @@ before_script:
2122
- sudo apt-get update > /dev/null
2223
- sudo apt-get install -y --force-yes php5-cgi php5-mysql
2324

24-
# create new site, stubbing sendmail path with true to prevent delivery errors and manually resolving drush path
25-
- mysql -e 'create database drupal'
26-
- php -d sendmail_path=`which true` `pear config-get php_dir`/drush/drush.php --yes core-quick-drupal --profile=testing --no-server --db-url=mysql://root:@127.0.0.1/drupal --enable=simpletest message
25+
# create new site, stubbing sendmail path with true to prevent delivery errors
26+
# and manually resolving drush path
27+
- mysql -e 'create database message'
28+
- php -d sendmail_path=`which true` `pear config-get php_dir`/drush/drush.php --yes core-quick-drupal --profile=testing --no-server --db-url=mysql://root:@127.0.0.1/message --enable=simpletest message
2729

28-
# reference and enable module in build site
30+
# reference and enable message in build site
2931
- ln -s $(readlink -e $(cd -)) message/drupal/sites/all/modules/message
3032
- cd message/drupal
31-
- drush --yes pm-enable message
33+
- drush --yes pm-enable message_example
3234

33-
# start a web server on port 8080, run in the background; wait for initialization
35+
# start a web server on port 8080, run in the background; wait for
36+
# initialization
3437
- drush runserver 127.0.0.1:8080 &
3538
- until netstat -an 2>/dev/null | grep '8080.*LISTEN'; do true; done
3639

README.md

+55-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
[![Build Status](https://travis-ci.org/Gizra/message.svg?branch=7.x-1.x)](https://travis-ci.org/Gizra/message)
22

3-
Overview
4-
========
53
A general logging utility that can be used as activity module.
64

5+
Overview
6+
========
77
* In message module, the arguments of a sentence can use tokens, custom
88
callbacks or be hard-coded. Making the arguments dynamic means that the
99
rendering time is slower than activity, on the other hand you can use
@@ -89,3 +89,56 @@ definition.
8989
"Override global settings" checkbox will make the global settings ignore the
9090
current message type and will allow to set purging definitions for the current
9191
type.
92+
93+
Computed arguments plugin
94+
============
95+
When saving a message to the DB the reference to the arguments callbacks stored
96+
in the DB as well. This will be a problem when we want to change the name of the
97+
function.
98+
99+
Message allows you to keep the message text with the arguments but the callbacks
100+
will be handled with a CTools plugin.
101+
102+
Message example define a 'Computed arguments' plugin for the 'Example arguments'
103+
message type. The name of the plugin need to be the name of the message type.
104+
105+
`example_arguments.inc`:
106+
```php
107+
<?php
108+
109+
$plugin = array(
110+
'label' => t('Message example create node'),
111+
'description' => t('Supply arguments handler for the message example create node bundle.'),
112+
'class' => 'ExampleArguments',
113+
);
114+
```
115+
116+
The class is implemented in `ExampleArguments.class.php`:
117+
```php
118+
<?php
119+
120+
class ExampleArguments extends MessageArgumentsBase {
121+
122+
/**
123+
* @return mixed
124+
*/
125+
public function getNameArgument() {
126+
return array(
127+
'@name' => array($this, 'processName'),
128+
'%time' => array($this, 'processTime'),
129+
'!link' => array($this, 'processLink'),
130+
);
131+
}
132+
```
133+
134+
The `getNameArgument` method define for each argument which method should handle
135+
it and calculate the value:
136+
```php
137+
/**
138+
* Process the current time.
139+
*/
140+
public function processTime() {
141+
return format_date($this->getMessage()->timestamp);
142+
}
143+
}
144+
```

includes/MessageArgumentInterface.php

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
/**
4+
* Contains MessageArgumentInterface.
5+
*/
6+
7+
interface MessageArgumentInterface {
8+
9+
/**
10+
* @return Message
11+
*/
12+
public function getMessage();
13+
14+
/**
15+
* @param Message $message
16+
* The message object.
17+
*
18+
* @return MessageArgumentsBase
19+
*/
20+
public function setMessage(Message $message);
21+
22+
/**
23+
* Retrieve the arguments info.
24+
*
25+
* @return array
26+
* The arguments as and their values.
27+
*/
28+
public function getArguments();
29+
30+
/**
31+
* The method return information about the arguments for the message and the
32+
* callbacks which responsible to compute the argument value.
33+
*
34+
* @return array
35+
* Associate array with the name of the argument and the callback
36+
* responsible to compute the argument value.
37+
*
38+
* @code
39+
* return array(
40+
* '@name' => array($this, 'processName'),
41+
* '%time' => array($this, 'processTime'),
42+
* '!link' => array($this, 'processLink'),
43+
* );
44+
* @endcode
45+
*
46+
* The callback will return the value for the argument. The message object can
47+
* be access via the getMessage() method.
48+
*/
49+
public function getNameArgument();
50+
}

includes/MessageArgumentsBase.php

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/**
4+
* Contains \MessageArgumentsBase.
5+
*/
6+
7+
abstract class MessageArgumentsBase implements MessageArgumentInterface {
8+
9+
/**
10+
* @var Message
11+
*
12+
* The message object.
13+
*/
14+
protected $message;
15+
16+
/**
17+
* {@inheritdoc}
18+
*/
19+
public function getMessage() {
20+
return $this->message;
21+
}
22+
23+
/**
24+
* {@inheritdoc}
25+
*/
26+
public function setMessage(Message $message) {
27+
$this->message = $message;
28+
return $this;
29+
}
30+
31+
/**
32+
* {@inheritdoc}
33+
*/
34+
public function getArguments() {
35+
$arguments = array();
36+
$callbacks = $this->getNameArgument();
37+
38+
foreach ($callbacks as $argument => $callback) {
39+
$arguments[$argument] = call_user_func($callback);
40+
}
41+
42+
return $arguments;
43+
}
44+
45+
}

includes/message.message.inc

+15-2
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ class Message extends Entity {
8585
}
8686

8787
/**
88+
* {@inheritdoc}
89+
*
8890
* Generate an array for rendering the entity's content.
8991
*
9092
* Iterate over the extra field settings, and show the visible partials.
@@ -140,10 +142,14 @@ class Message extends Entity {
140142
* Replace arguments with their placeholders.
141143
*
142144
* @param $langcode
143-
* Optional; The language to get the text in. If not set the current language
144-
* will be used.
145+
* Optional; The language to get the text in. If not set the current
146+
* language will be used.
145147
* @param $options
146148
* Optional; Array to be passed to MessageType::getText().
149+
*
150+
* @return mixed|string
151+
* The message text compiled into a string or an array of the message
152+
* partials.
147153
*/
148154
public function getText($langcode = LANGUAGE_NONE, $options = array()) {
149155
$message_type = $this->getType();
@@ -154,6 +160,13 @@ class Message extends Entity {
154160
}
155161

156162
$arguments = message_get_property_values($this, 'arguments');
163+
164+
// Check if the current message have any arguments plugin.
165+
$handler = message_get_computed_arguments_handler($this);
166+
if ($handler && $handler_arguments = $handler->getArguments()) {
167+
$arguments = array_merge($arguments, $handler_arguments);
168+
}
169+
157170
$output = $message_type->getText($langcode, $options);
158171

159172
if (!empty($arguments)) {

message.api.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ function hook_message_view_alter(&$build) {
7272
* @see hook_default_message_type_alter()
7373
*/
7474
function hook_default_message_type() {
75-
$defaults['main'] = entity_create('message_type', array(
75+
$defaults['main'] = message_type_create('example_create_node', array(
7676
'description' => 'Type description',
7777
'argument_keys' => array(
7878
'!teaser',

message.info

+13-7
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
1-
; $Id: message.info,v 1.3 2010/06/07 10:54:11 amitaibu Exp $
21
name = Message
32
description = "A general message logging utility."
43
core = 7.x
54
package = Message
5+
66
dependencies[] = entity
7+
dependencies[] = ctools
78

8-
; Entity API includes
9+
; Entity API includes.
910
files[] = message.info.inc
1011
files[] = includes/message.admin.inc
1112
files[] = includes/message.exception.inc
12-
files[] = includes/message.message_type_category.inc
13-
files[] = includes/message.message_type.inc
1413
files[] = includes/message.message.inc
14+
files[] = includes/message.message_type.inc
15+
files[] = includes/message.message_type_category.inc
1516

16-
; Tests
17-
files[] = message.test
17+
; Tests.
18+
files[] = tests/message.test
19+
files[] = tests/MessageArgumentsTestCase.test
1820

19-
; Views includes
21+
; Views includes.
2022
files[] = includes/views/message.views.inc
2123
files[] = includes/views/handlers/message_handler_field_message_render.inc
2224
files[] = includes/views/handlers/message_handler_filter_message_type.inc
2325
files[] = includes/views/handlers/message_handler_filter_message_type_category.inc
26+
27+
; Message arguments handlers.
28+
files[] = includes/MessageArgumentsBase.php
29+
files[] = includes/MessageArgumentInterface.php

message.module

+48
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,52 @@ define('MESSAGE_FIELD_MESSAGE_TEXT', 'message_text');
1515
*/
1616
define('MESSAGE_PURGE_LIMIT', 100);
1717

18+
/**
19+
* Implements hook_ctools_plugin_type().
20+
*/
21+
function message_ctools_plugin_type() {
22+
$plugins['computed_arguments'] = array(
23+
'classes' => array('class'),
24+
'child plugins' => TRUE,
25+
);
26+
27+
return $plugins;
28+
}
29+
30+
/**
31+
* Return message arguments plugin by the message type.
32+
*
33+
* @param Message $message
34+
* The message object.
35+
*
36+
* @return MessageArgumentsBase | NULL
37+
* The handler object if found else NULL.
38+
*/
39+
function message_get_computed_arguments_handler(Message $message) {
40+
$plugin = message_get_computed_arguments_plugin($message->bundle());
41+
42+
if (!$class = ctools_plugin_load_class('message', 'computed_arguments', $message->bundle(), 'class')) {
43+
return NULL;
44+
}
45+
46+
$handler = new $class($plugin);
47+
return $handler->setMessage($message);
48+
}
49+
50+
/**
51+
* Get the specified message arguments plugin.
52+
*
53+
* @param string $plugin_name
54+
* Optional. If provided the function will return the desired plugin.
55+
*
56+
* @return array
57+
* The selected plugin for the message arguments.
58+
*/
59+
function message_get_computed_arguments_plugin($plugin_name = '') {
60+
ctools_include('plugins');
61+
return ctools_get_plugins('message', 'computed_arguments', $plugin_name);
62+
}
63+
1864
/**
1965
* Implementation of hook_views_api().
2066
*/
@@ -835,6 +881,8 @@ function message_type_delete($message) {
835881
* @param $account
836882
* Optional; The user object to associate the message with. If empty, the
837883
* current user will be used.
884+
*
885+
* @return Message
838886
*/
839887
function message_create($type, $values = array(), $account = NULL) {
840888
global $language;

0 commit comments

Comments
 (0)