Repository URL to install this package:
|
Version:
1.0.1 ▾
|
<?php
namespace Drupal\dds_devel\Form;
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Core\Config\Entity\ConfigEntityType;
use Drupal\Core\Field\FieldItemList;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\entity_reference_revisions\Plugin\Field\FieldType\EntityReferenceRevisionsItem;
use Drupal\node\Entity\Node;
use Drupal\paragraphs\Entity\Paragraph;
class ParagraphUsageForm extends FormBase {
/**
* Returns a unique string identifying the form.
*
* The returned ID should be a unique string that can be a valid PHP function
* name, since it's used in hook implementation names such as
* hook_form_FORM_ID_alter().
*
* @return string
* The unique string identifying the form.
*/
public function getFormId() {
return 'dds_devel_paragraph_usage';
}
/**
* Form constructor.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @return array
* The form structure.
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form = [];
$type = $form_state->getValue('type');
$paragraph_types = \Drupal::service('entity_type.bundle.info')->getBundleInfo('paragraph');
$options = [];
foreach ($paragraph_types as $paragraph_type_id => $paragraph_type) {
$options[$paragraph_type_id] = $paragraph_type['label'];
}
$form['type'] = [
'#type' => 'select',
'#options' => $options,
'#default_value' => $type
];
$form['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Find usage')
];
if (!empty($type)) {
$ids = \Drupal::entityQuery('paragraph')
->condition('type', $type)
->execute();
try {
$entities = \Drupal::entityTypeManager()->getStorage('paragraph')
->loadMultiple($ids);
} catch (InvalidPluginDefinitionException $e) {
// TODO: Error logging
} catch (PluginNotFoundException $e) {
// TODO: Error logging
}
while (!empty($ids)) {
$ids = []; $new = [];
/** @var Paragraph $paragraph */
foreach ($entities as $id => $paragraph) {
$parent = $paragraph->getParentEntity();
if (!is_null($parent) && $parent->getEntityTypeId() == 'paragraph') {
$new[$parent->id()] = $parent->id();
unset($entities[$id]);
}
}
if (!empty($new)) {
foreach ($new as $id) {
$ids[] = $id;
}
try {
$new_entities = \Drupal::entityTypeManager()->getStorage('paragraph')
->loadMultiple($ids);
} catch (InvalidPluginDefinitionException $e) {
// TODO: Error logging
} catch (PluginNotFoundException $e) {
// TODO: Error logging
}
$entities += $new_entities;
}
}
$ids = array_keys($entities);
$form['list'] = array(
'#type' => 'table',
'#header' => array(
t('Title'),
t('Link'),
t('Count'),
t('Published')
),
'#empty' => t('No usage was found.'),
// TableSelect: Injects a first column containing the selection widget into
// each table row.
// Note that you also need to set #tableselect on each form submit button
// that relies on non-empty selection values (see below).
'#tableselect' => FALSE,
);
/** @var Paragraph $paragraph */
foreach ($entities as $id => $paragraph) {
$parent = $paragraph->getParentEntity();
if (!is_null($parent) && $parent->getEntityTypeId() == 'node') {
/** @var Node $node */
$node = $parent;
$count = $this->getCount($node, $type, $ids);
if ($count > 0) {
$form['list'][$node->id()]['title'] = [
'#plain_text' => $node->label(),
];
$alias = \Drupal::service('path.alias_manager')
->getAliasByPath('/node/' . $node->id());
$url = customeredit_textToUrl($alias);
if ($url !== FALSE) {
$form['list'][$node->id()]['link'] = [
'#type' => 'link',
'#title' => $alias,
'#url' => $url,
];
}
$form['list'][$node->id()]['count'] = [
'#plain_text' => $count
];
$form['list'][$node->id()]['published'] = [
'#plain_text' => $node->isPublished() ? 'Published' : 'Unpublished'
];
}
}
}
} else {
$form['list'] = [
'#type' => 'hidden'
];
}
return $form;
}
/**
* Form submission handler.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$form_state->setRebuild();
}
/**
* @param \Drupal\node\Entity\Node $node
* @param $type
*
* @return int
*/
protected function getCount(Node $node, $type, $ids = []) {
$count = 0;
/** @var FieldItemList $field */
foreach ($node->getFields() as $field) {
if ($field->getFieldDefinition()->getType() == 'entity_reference_revisions') {
/** @var \Drupal\entity_reference_revisions\EntityReferenceRevisionsFieldItemList $paragraphs */
$paragraphs = $field;
if ($paragraphs->getSetting('target_type') == 'paragraph') {
/** @var EntityReferenceRevisionsItem $item */
foreach ($paragraphs as $item) {
if (!empty($item->getValue()['entity'])) {
/** @var Paragraph $paragraph */
$paragraph = $item->getValue()['entity'];
if ($paragraph->bundle() == $type) {
$count++;
}
} elseif (in_array($item->getValue()['target_id'], $ids)) {
$count++;
}
}
}
}
}
return $count;
}
}