summaryrefslogtreecommitdiffstats
path: root/src/ContentConnectedManager.php
blob: 074710ac45e1240127b2b864d99ec3963dc567f9 (plain)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<?php


namespace Drupal\content_connected;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\Query\QueryFactory;
use Drupal\Core\Extension\ModuleHandler;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Link;

/**
 * Class ContentConnectedManager.
 *
 * @package Drupal\content_connected
 */
class ContentConnectedManager implements ContentConnectedManagerInterface {

  use StringTranslationTrait;

  use DependencySerializationTrait {
    __wakeup as defaultWakeup;
    __sleep as defaultSleep;
  }

  /**
   * Entity manager service.
   *
   * @var \Drupal\Core\Entity\EntityManagerInterface
   */
  protected $entityManager;

  /**
   * Forum settings config object.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * The entity query factory service.
   *
   * @var \Drupal\Core\Entity\Query\QueryFactory
   */
  protected $entityQuery;

  /**
   * The renderer.
   *
   * @var \Drupal\Core\Render\RendererInterface
   */
  protected $renderer;

  /**
   * Drupal\Core\Extension\ModuleHandler definition.
   *
   * @var ModuleHandler
   */
  protected $moduleHandler;

  /**
   * Constructs the content connected manager service.
   *
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
   *   The entity manager service.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory service.
   * @param \Drupal\Core\Entity\Query\QueryFactory $query_factory
   *   The entity query factory service.
   * @param \Drupal\Core\Extension\ModuleHandler $module_handler
   *   The module handler service.
   * @param \Drupal\Core\Render\RendererInterface $renderer
   *   The render service.
   * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
   *   The translation manager service.
   */
  public function __construct(EntityManagerInterface $entity_manager, ConfigFactoryInterface $config_factory, QueryFactory $query_factory, ModuleHandler $module_handler, RendererInterface $renderer, TranslationInterface $string_translation) {
    $this->entityManager = $entity_manager;
    $this->configFactory = $config_factory;
    $this->entityQuery = $query_factory;
    $this->moduleHandler = $module_handler;
    $this->renderer = $renderer;
    $this->stringTranslation = $string_translation;
  }

  /**
   * {@inheritdoc}
   */
  public function getEntityRefrenceFields() {

    $fields = array();
    $ref_fields = $this->entityManager->getStorage('field_storage_config')->loadByProperties(
      array(
        'settings' => array(
          'target_type' => 'node',
        ),
        'entity_type' => 'node',
        'type' => 'entity_reference',
        'deleted' => FALSE,
        'status' => 1,
      )
    );
    foreach ($ref_fields as $field) {
      $fields[$field->getName()] = $field->getLabel();
    }
    return $fields;
  }

  /**
   * {@inheritdoc}
   */
  public function getlinkFields() {
    $fields = array();
    $link_fields = $this->entityManager->getStorage('field_storage_config')->loadByProperties(
      array(
        'entity_type' => 'node',
        'type' => 'link',
        'deleted' => FALSE,
        'status' => 1,
      )
    );
    foreach ($link_fields as $field) {
      $fields[$field->getName()] = $field->getLabel();
    }
    return $fields;
  }

  /**
   * {@inheritdoc}
   */
  public function getLongTextFields() {
    $fields = array();
    $text_with_summary = $this->entityManager->getStorage('field_storage_config')->loadByProperties(
      array(
        'entity_type' => 'node',
        'type' => 'text_with_summary',
        'deleted' => FALSE,
        'status' => 1,
      )
    );
    $text_long = $this->entityManager->getStorage('field_storage_config')->loadByProperties(
      array(
        'entity_type' => 'node',
        'type' => 'text_long',
        'deleted' => FALSE,
        'status' => 1,
      )
    );

    $long_fields = array_merge($text_with_summary, $text_long);
    foreach ($long_fields as $field) {
      $fields[$field->getName()] = $field->getLabel();
    }
    return $fields;
  }

  /**
   * {@inheritdoc}
   */
  protected function matchesEntityRefrenceField($nid) {

    $matches = array();
    $fields = $this->getEntityRefrenceFields();

    $config = $this->configFactory->get('content_connected.adminsettings');
    $exclude_fields = $config->get('content_connected_exclude_entityreffields');

    $fields = $this->excludeFields($fields, $exclude_fields);

    foreach ($fields as $fieldname => $field_value) {
      $matches[$fieldname] = $this->entityQuery->get('node')
          ->condition('status', 1)
          ->condition($fieldname, $nid)->execute();
    }

    return array('entity_reference' => $matches);
  }

  /**
   * {@inheritdoc}
   */
  protected function matchesLinkField($nid) {
    $matches = array();

    $fields = $this->getlinkFields();
    $node = $this->entityManager->getStorage('node')->load($nid);
    $config = $this->configFactory->get('content_connected.adminsettings');
    $exclude_fields = $config->get('content_connected_exclude_linkfields');

    $fields = $this->excludeFields($fields, $exclude_fields);

    foreach ($fields as $fieldname => $field_value) {

      $matches[$fieldname] = $this->entityQuery->get('node')
          ->condition('status', 1)
          ->condition($fieldname, 'node/' . $node->id(), 'CONTAINS')->execute();
    }
    return array('Link_field' => $matches);
  }

  /**
   * {@inheritdoc}
   */
  protected function matchesLongTextField($nid) {
    $matches = array();
    $fields = $this->getLongTextFields();

    $node = $this->entityManager->getStorage('node')->load($nid);
    $config = $this->configFactory->get('content_connected.adminsettings');
    $exclude_fields = $config->get('content_connected_exclude_longtextfields');

    $fields = $this->excludeFields($fields, $exclude_fields);

    foreach ($fields as $fieldname => $field_value) {
      $matches[$fieldname] = $this->entityQuery->get('node')
          ->condition('status', 1)
          ->condition($fieldname, 'node/' . $node->id(), 'CONTAINS')->execute();
    }
    return array('long_text' => $matches);
  }

  /**
   * Render function to generate content connected table.
   */
  public function renderMatches($nid) {

    $matches = array_merge($this->matchesEntityRefrenceField($nid), $this->matchesLinkField($nid), $this->matchesLongTextField($nid));

    // Allow other modules to alter the reference array.
    $this->moduleHandler->alter('content_connected', $nid, $matches);
    $headers = array(
      $this->t('Title'),
      $this->t('Content type'),
      $this->t('Status'),
      $this->t('Type of field'),
    );
    $rows = array();
    foreach ($matches as $match_type => $match) {
      foreach ($match as $field_name => $nids) {
        $nodes = $this->entityManager->getStorage('node')->loadMultiple($nids);

        foreach ($nodes as $node) {
          $row = array();
          $link = Link::fromTextAndUrl($node->label(), $node->toUrl());
          $row[] = $link;
          $row[] = node_get_type_label($node);
          $row[] = $node->isPublished() == NODE_PUBLISHED ? $this->t('Published') : $this->t('Not published');
          $row[] = $match_type . '(' . $field_name . ')';
          $rows[] = $row;
        }
      }
    }

    $build = array(
      '#theme' => 'table',
      '#caption' => $this->t('Content connected'),
      '#header' => $headers,
      '#rows' => $rows,
      '#empty' => $this->t('No content connected available.'),
    );

    return $this->renderer->render($build);
  }

  /**
   * Helper function to exclude fields.
   */
  protected function excludeFields($fields, $exclude_fields) {

    if (!empty($exclude_fields)) {
      $fields = array_diff_key($fields, array_filter($exclude_fields));
    }
    return $fields;
  }

}