-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
DraggableViewsTrait.php
98 lines (79 loc) · 2.59 KB
/
DraggableViewsTrait.php
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
<?php
declare(strict_types=1);
namespace DrevOps\BehatSteps;
use Behat\Gherkin\Node\TableNode;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Database\Database;
use Drupal\node\Entity\Node;
use Drupal\node\NodeInterface;
/**
* Trait DraggableViewsTrait.
*
* Draggable Views-related steps.
*
* @note This is currently limited to nodes only.
*/
trait DraggableViewsTrait {
/**
* Save order of the Draggable Order items.
*
* @Then I save draggable views :view_id view :views_display_id display :bundle items in the following order:
*/
public function draggableViewsSaveBundleOrder(string $view_id, string $view_display_id, string $bundle, TableNode $order_table): void {
$connection = Database::getConnection();
foreach ($order_table->getColumn(0) as $weight => $title) {
$node = $this->draggableViewsFindNode($bundle, ['title' => $title]);
if (empty($node)) {
throw new \RuntimeException(sprintf('Unable to find node "%s"', $title));
}
$entity_id = $node->id();
// Here and below: copied from draggableviews_views_submit().
// Remove old data.
$connection->delete('draggableviews_structure')
->condition('view_name', $view_id)
->condition('view_display', $view_display_id)
->condition('entity_id', $entity_id)
->execute();
// Add new data.
$record = [
'view_name' => $view_id,
'view_display' => $view_display_id,
'args' => '[]',
'entity_id' => $entity_id,
'weight' => $weight,
];
$connection->insert('draggableviews_structure')->fields($record)->execute();
}
// We invalidate the entity list cache, so other views are also aware of the
// cache.
$list_cache_tags = \Drupal::entityTypeManager()->getDefinition('node')->getListCacheTags();
Cache::invalidateTags($list_cache_tags);
}
/**
* Find a node using provided conditions.
*
* @param string $type
* The node type.
* @param array<string, string> $conditions
* The conditions to search for.
*
* @return \Drupal\node\NodeInterface|null
* The found node or NULL.
*/
protected function draggableViewsFindNode(string $type, array $conditions): ?NodeInterface {
$query = \Drupal::entityQuery('node')
->accessCheck(FALSE)
->condition('type', $type);
foreach ($conditions as $k => $v) {
$and = $query->andConditionGroup();
$and->condition($k, $v);
$query->condition($and);
}
$nids = $query->execute();
if (empty($nids)) {
return NULL;
}
$nid = current($nids);
return Node::load($nid);
}
}