comparison core/modules/layout_builder/src/InlineBlockUsage.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents a9cd425dd02b
children
comparison
equal deleted inserted replaced
4:a9cd425dd02b 5:12f9dff5fda9
5 use Drupal\Core\Database\Connection; 5 use Drupal\Core\Database\Connection;
6 use Drupal\Core\Entity\EntityInterface; 6 use Drupal\Core\Entity\EntityInterface;
7 7
8 /** 8 /**
9 * Service class to track inline block usage. 9 * Service class to track inline block usage.
10 *
11 * @internal
12 */ 10 */
13 class InlineBlockUsage { 11 class InlineBlockUsage implements InlineBlockUsageInterface {
14 12
15 /** 13 /**
16 * The database connection. 14 * The database connection.
17 * 15 *
18 * @var \Drupal\Core\Database\Connection 16 * @var \Drupal\Core\Database\Connection
28 public function __construct(Connection $database) { 26 public function __construct(Connection $database) {
29 $this->database = $database; 27 $this->database = $database;
30 } 28 }
31 29
32 /** 30 /**
33 * Adds a usage record. 31 * {@inheritdoc}
34 *
35 * @param int $block_content_id
36 * The block content id.
37 * @param \Drupal\Core\Entity\EntityInterface $entity
38 * The layout entity.
39 */ 32 */
40 public function addUsage($block_content_id, EntityInterface $entity) { 33 public function addUsage($block_content_id, EntityInterface $entity) {
41 $this->database->merge('inline_block_usage') 34 $this->database->merge('inline_block_usage')
42 ->keys([ 35 ->keys([
43 'block_content_id' => $block_content_id, 36 'block_content_id' => $block_content_id,
45 'layout_entity_type' => $entity->getEntityTypeId(), 38 'layout_entity_type' => $entity->getEntityTypeId(),
46 ])->execute(); 39 ])->execute();
47 } 40 }
48 41
49 /** 42 /**
50 * Gets unused inline block IDs. 43 * {@inheritdoc}
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 */ 44 */
58 public function getUnused($limit = 100) { 45 public function getUnused($limit = 100) {
59 $query = $this->database->select('inline_block_usage', 't'); 46 $query = $this->database->select('inline_block_usage', 't');
60 $query->fields('t', ['block_content_id']); 47 $query->fields('t', ['block_content_id']);
61 $query->isNull('layout_entity_id'); 48 $query->isNull('layout_entity_id');
62 $query->isNull('layout_entity_type'); 49 $query->isNull('layout_entity_type');
63 return $query->range(0, $limit)->execute()->fetchCol(); 50 return $query->range(0, $limit)->execute()->fetchCol();
64 } 51 }
65 52
66 /** 53 /**
67 * Remove usage record by layout entity. 54 * {@inheritdoc}
68 *
69 * @param \Drupal\Core\Entity\EntityInterface $entity
70 * The layout entity.
71 */ 55 */
72 public function removeByLayoutEntity(EntityInterface $entity) { 56 public function removeByLayoutEntity(EntityInterface $entity) {
73 $query = $this->database->update('inline_block_usage') 57 $query = $this->database->update('inline_block_usage')
74 ->fields([ 58 ->fields([
75 'layout_entity_type' => NULL, 59 'layout_entity_type' => NULL,
79 $query->condition('layout_entity_id', $entity->id()); 63 $query->condition('layout_entity_id', $entity->id());
80 $query->execute(); 64 $query->execute();
81 } 65 }
82 66
83 /** 67 /**
84 * Delete the inline blocks' the usage records. 68 * {@inheritdoc}
85 *
86 * @param int[] $block_content_ids
87 * The block content entity IDs.
88 */ 69 */
89 public function deleteUsage(array $block_content_ids) { 70 public function deleteUsage(array $block_content_ids) {
90 if (!empty($block_content_ids)) { 71 if (!empty($block_content_ids)) {
91 $query = $this->database->delete('inline_block_usage')->condition('block_content_id', $block_content_ids, 'IN'); 72 $query = $this->database->delete('inline_block_usage')->condition('block_content_id', $block_content_ids, 'IN');
92 $query->execute(); 73 $query->execute();
93 } 74 }
94 } 75 }
95 76
96 /** 77 /**
97 * Gets usage record for inline block by ID. 78 * {@inheritdoc}
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 */ 79 */
105 public function getUsage($block_content_id) { 80 public function getUsage($block_content_id) {
106 $query = $this->database->select('inline_block_usage'); 81 $query = $this->database->select('inline_block_usage');
107 $query->condition('block_content_id', $block_content_id); 82 $query->condition('block_content_id', $block_content_id);
108 $query->fields('inline_block_usage', ['layout_entity_id', 'layout_entity_type']); 83 $query->fields('inline_block_usage', ['layout_entity_id', 'layout_entity_type']);