Mercurial > hg > cmmr2012-drupal-site
comparison core/modules/layout_builder/src/InlineBlockUsage.php @ 4:a9cd425dd02b
Update, including to Drupal core 8.6.10
author | Chris Cannam |
---|---|
date | Thu, 28 Feb 2019 13:11:55 +0000 |
parents | |
children | 12f9dff5fda9 |
comparison
equal
deleted
inserted
replaced
3:307d7a7fd348 | 4:a9cd425dd02b |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\layout_builder; | |
4 | |
5 use Drupal\Core\Database\Connection; | |
6 use Drupal\Core\Entity\EntityInterface; | |
7 | |
8 /** | |
9 * Service class to track inline block usage. | |
10 * | |
11 * @internal | |
12 */ | |
13 class InlineBlockUsage { | |
14 | |
15 /** | |
16 * The database connection. | |
17 * | |
18 * @var \Drupal\Core\Database\Connection | |
19 */ | |
20 protected $database; | |
21 | |
22 /** | |
23 * Creates an InlineBlockUsage object. | |
24 * | |
25 * @param \Drupal\Core\Database\Connection $database | |
26 * The database connection. | |
27 */ | |
28 public function __construct(Connection $database) { | |
29 $this->database = $database; | |
30 } | |
31 | |
32 /** | |
33 * Adds a usage record. | |
34 * | |
35 * @param int $block_content_id | |
36 * The block content id. | |
37 * @param \Drupal\Core\Entity\EntityInterface $entity | |
38 * The layout entity. | |
39 */ | |
40 public function addUsage($block_content_id, EntityInterface $entity) { | |
41 $this->database->merge('inline_block_usage') | |
42 ->keys([ | |
43 'block_content_id' => $block_content_id, | |
44 'layout_entity_id' => $entity->id(), | |
45 'layout_entity_type' => $entity->getEntityTypeId(), | |
46 ])->execute(); | |
47 } | |
48 | |
49 /** | |
50 * Gets unused inline block IDs. | |
51 * | |
52 * @param int $limit | |
53 * The maximum number of block content entity IDs to return. | |
54 * | |
55 * @return int[] | |
56 * The entity IDs. | |
57 */ | |
58 public function getUnused($limit = 100) { | |
59 $query = $this->database->select('inline_block_usage', 't'); | |
60 $query->fields('t', ['block_content_id']); | |
61 $query->isNull('layout_entity_id'); | |
62 $query->isNull('layout_entity_type'); | |
63 return $query->range(0, $limit)->execute()->fetchCol(); | |
64 } | |
65 | |
66 /** | |
67 * Remove usage record by layout entity. | |
68 * | |
69 * @param \Drupal\Core\Entity\EntityInterface $entity | |
70 * The layout entity. | |
71 */ | |
72 public function removeByLayoutEntity(EntityInterface $entity) { | |
73 $query = $this->database->update('inline_block_usage') | |
74 ->fields([ | |
75 'layout_entity_type' => NULL, | |
76 'layout_entity_id' => NULL, | |
77 ]); | |
78 $query->condition('layout_entity_type', $entity->getEntityTypeId()); | |
79 $query->condition('layout_entity_id', $entity->id()); | |
80 $query->execute(); | |
81 } | |
82 | |
83 /** | |
84 * Delete the inline blocks' the usage records. | |
85 * | |
86 * @param int[] $block_content_ids | |
87 * The block content entity IDs. | |
88 */ | |
89 public function deleteUsage(array $block_content_ids) { | |
90 if (!empty($block_content_ids)) { | |
91 $query = $this->database->delete('inline_block_usage')->condition('block_content_id', $block_content_ids, 'IN'); | |
92 $query->execute(); | |
93 } | |
94 } | |
95 | |
96 /** | |
97 * Gets usage record for inline block by ID. | |
98 * | |
99 * @param int $block_content_id | |
100 * The block content entity ID. | |
101 * | |
102 * @return object | |
103 * The usage record with properties layout_entity_id and layout_entity_type. | |
104 */ | |
105 public function getUsage($block_content_id) { | |
106 $query = $this->database->select('inline_block_usage'); | |
107 $query->condition('block_content_id', $block_content_id); | |
108 $query->fields('inline_block_usage', ['layout_entity_id', 'layout_entity_type']); | |
109 $query->range(0, 1); | |
110 return $query->execute()->fetchObject(); | |
111 } | |
112 | |
113 } |