-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathplug_example.module
103 lines (95 loc) · 2.76 KB
/
plug_example.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
/**
* @file
* Module implementation file.
*/
use \Drupal\plug_example\NamePluginManager;
use \Drupal\plug_example\FruitPluginManager;
/**
* Implements hook_menu().
*/
function plug_example_menu() {
$items['plug/test'] = array(
'title' => 'Plugins example',
'description' => 'Plugins example page.',
'page callback' => 'plug_example_annotation_test_page',
'access callback' => TRUE,
'type' => MENU_NORMAL_ITEM,
);
$items['plug/test/annotation'] = array(
'title' => 'Annotation',
'description' => 'Plugins annotation page.',
'page callback' => 'plug_example_annotation_test_page',
'access callback' => TRUE,
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['plug/test/yaml'] = array(
'title' => 'YAML',
'description' => 'Plugins YAML page.',
'page callback' => 'plug_example_yaml_test_page',
'access callback' => TRUE,
'type' => MENU_LOCAL_TASK,
);
return $items;
}
/**
* Implements hook_fruit_plugin_alter().
*
* Adds a new fruit plugin programmatically.
*/
function plug_example_fruit_plugin_alter(&$plugins) {
$plugin_manager = FruitPluginManager::create();
$base_definition = array(
'label' => 'Banana',
'sugar' => 'medium',
'id' => 'banana',
'provider' => 'plug_example',
);
$plugin_manager->processDefinition($base_definition);
$plugins[$base_definition['id']] = $base_definition;
}
/**
* Implements hook_name_plugin_alter().
*
* Modifies the plugin definition programmatically. John is not a company!
*/
function plug_example_name_plugin_alter(&$plugins) {
$plugins['john']['company'] = FALSE;
}
/**
* Page callback to test annotation the plugins.
*/
function plug_example_annotation_test_page() {
$manager = NamePluginManager::create();
return plug_example_test_page_content($manager, 'displayName');
}
/**
* Page callback to test the plugins.
*/
function plug_example_yaml_test_page() {
$manager = FruitPluginManager::create();
return plug_example_test_page_content($manager, 'display');
}
/**
* Generates the plug_example page content.
*
* @param \Drupal\Component\Plugin\PluginManagerInterface $manager
* The Plugin manager.
*
* @return array
* The test page render array.
*/
function plug_example_test_page_content($manager, $method) {
$items = array();
foreach ($manager->getDefinitions() as $id => $plugin) {
// This is just a silly way to show how you can pass arbitrary configuration
// at runtime. If true, the name will be wrapped in <em></em>.
$random_bool = (bool) floor(mt_rand(0, 2));
$instance = $manager->createInstance($id, array('em' => $random_bool));
$items[] = $instance->{$method}() . '<br /><pre>' . var_export($instance, TRUE) . '</pre>';
}
return array(
'#theme' => 'item_list',
'#items' => $items,
);
}