-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswitch_node_input_filters.drush.inc
81 lines (71 loc) · 2.42 KB
/
switch_node_input_filters.drush.inc
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
<?php
/**
* Implements hook_drush_command().
*/
function switch_node_input_filters_drush_command() {
$items = array();
$items['switch-node-input-filters'] = array(
'description' => 'Switch input filters on nodes of a given type.',
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
'arguments' => array(
'node-type' => 'The type of node on which filters should be switched.',
'to-filter' => 'Switch nodes to this filter...',
'from-filter' => '...from this filter (optional - without this, all nodes will be switched).',
),
'options' => array(
'limit' => 'todo',
'dry-run' => 'todo',
),
'required-arguments' => 2,
'examples' => array(
'drush snif Article basic_html full_html' => 'Switch all Article nodes which currently use full_html to basic_html.',
'drush snif Blog wysiwyg' => 'Switch all Blog nodes to the wysiwyg filter.',
),
'aliases' => array('snif'),
);
return $items;
}
function drush_switch_node_input_filters($node_type, $to_filter, $from_filter = NULL) {
// @todo: check / sanitise inputs.
// Get a list of nodes we want to switch
$query = \Drupal::entityQuery('node')
->condition('type', $node_type);
// @todo: --limit option
// ->range(0, 2);
// Run the query as user 1.
// ->addMetaData('account', user_load(1));
if (!is_null($from_filter)) {
// Add a condition based on existing input filter
$query->condition('body.format', $from_filter);
}
$nids = $query->execute();
$matched_nodes = count($nids);
$node_storage = \Drupal::entityManager()->getStorage('node');
// Process one node at a time.
$switched_nodes = 0;
foreach ($nids as $nid) {
$node = $node_storage->load($nid);
$old_filter = $node->body->format;
$log_args = array(
'@nid' => $nid,
'@old' => $old_filter,
'@to' => $to_filter,
);
if ($to_filter != $old_filter) {
$node->body->format = $to_filter;
$node->save();
$switched_nodes++;
drush_log(dt('Switched node nid=@nid from filter @old to @to.', $log_args));
}
else {
drush_log(dt('Node nid=@nid was already using the @to filter.', $log_args));
}
}
$log_args = array(
'@type' => $node_type,
'@switched' => $switched_nodes,
'@matched' => $matched_nodes,
'@filter' => $to_filter,
);
drush_log(dt('Switched @switched @type nodes (out of @matched matches) to @filter.', $log_args), 'ok');
}