Commit 7e6258c3 authored by Urvashi Vora's avatar Urvashi Vora Committed by emarcarelli
Browse files

Issue #3282947 by urvashi_vora: Drupal Coding Standard

parent c01a1acf
Loading
Loading
Loading
Loading
+11 −3
Changes for paragraph_instances.module: 11 added lines, 3 removed lines.
Original line number Diff line number Diff line
<?php

/**
 * @file
 * Module file.
 */

/**
 * Implement paragraph instance theme.
 */
function paragraph_instances_theme() {
  return [
    'find-instances-tab' => [
      'variables' => [
        'instances' => [],
      ]
      ],
    ],
    'find-instances-page' => [
      'variables' => [
        'instances' => [],
        'type' => null,
        'type' => NULL,
        'paragraphs' => [],
      ]
      ],
    ],
  ];
}
+81 −13
Changes for src/Controller/InstancesController.php: 81 added lines, 13 removed lines.
Original line number Diff line number Diff line
<?php

namespace Drupal\paragraph_instances\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Url;
use Drupal\paragraphs\Entity\ParagraphsType;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\HttpFoundation\RequestStack;

/**
 * Implements class Intances controller.
 */
class InstancesController extends ControllerBase {

  /**
   * The database connection to be used.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $database;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The request stack.
   *
   * @var \Symfony\Component\HttpFoundation\RequestStack
   */
  protected $requestStack;

  /**
   * Dependency injection implementation .
   *
   * @param \Drupal\Core\Database\Connection $database
   *   The database connection to be used.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   The request stack.
   */
  public function __construct(Connection $database, EntityTypeManagerInterface $entity_type_manager, RequestStack $request_stack) {
    $this->database = $database;
    $this->entityTypeManager = $entity_type_manager;
    $this->requestStack = $request_stack;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    $container->get('database');
    $container->get('entity_type.manager');
    $container->get('request_stack');
  }

  /**
   * {@inheritdoc}
   */
  public function findInstancesTab() {
    $path = \Drupal::request()->getpathInfo();
    $path = $this->requestStack->getpathInfo();
    $arg = explode('/', $path);
    $paragraphType = $arg[4];
    $nodes = $this->getInstances($paragraphType);
@@ -18,9 +74,13 @@ class InstancesController extends ControllerBase {
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function findInstancesPage() {
    $paragraphTypes = ParagraphsType::loadMultiple();
    $paragraphType = $_REQUEST['type'] ?? null;
    $rstack = $this->requestStack->getCurrentRequest()->request->get('type');
    $paragraphType = $rstack ?? NULL;
    $nodes = $this->getInstances($paragraphType);

    return [
@@ -31,20 +91,24 @@ class InstancesController extends ControllerBase {
    ];
  }

  /**
   * {@inheritdoc}
   */
  protected function getInstances($paragraphType) {
    $database = \Drupal::database();
    $database = $this->database;
    $nodes = [];
    $nids = [];

    if ($paragraphType) {
      $instances = $database->query('select * FROM {paragraphs_item_field_data} where status = 1 and type = :type', [
        ':type' => $paragraphType
        ':type' => $paragraphType,
      ])->fetchAll();

      foreach ($instances as $instance) {
        // Check if the content still exists, because paragraphs_item_field_data is apparently not cleaned up after deleting content
        // Check if the content still exists, because paragraphs_item_field_data
        // is apparently not cleaned up after deleting content.
        $uses = $database->query('select * FROM {' . ($instance->parent_type == 'paragraph' ? 'paragraph' : 'node') . '__' . $instance->parent_field_name . '} where ' . $instance->parent_field_name . '_target_id = :id', [
          ':id' => $instance->id
          ':id' => $instance->id,
        ])->fetch();
        if (empty($uses)) {
          continue;
@@ -62,9 +126,9 @@ class InstancesController extends ControllerBase {
            $nids[] = $nid;
            $nodes[] = [
              'id' => $nid,
              'node' => \Drupal::entityTypeManager()->getStorage('node')->load($nid),
              'url' => 'https://' . \Drupal::request()->getHost() . \Drupal::service('path_alias.manager')->getAliasByPath('/node/' . $nid),
              'count' => 1
              'node' => $this->entityTypeManager->getStorage('node')->load($nid),
              'url' => 'https://' . $this->requestStack->getHost() . \Drupal::service('path_alias.manager')->getAliasByPath('/node/' . $nid),
              'count' => 1,
            ];
          }
          else {
@@ -82,11 +146,14 @@ class InstancesController extends ControllerBase {
    return $nodes;
  }

  /**
   * {@inheritdoc}
   */
  protected function getParentNodeId($id) {
    $database = \Drupal::database();
    $database = $this->database;

    $instances = $database->query('select * FROM {paragraphs_item_field_data} where id = :id', [
      ':id' => $id
      ':id' => $id,
    ])->fetchAll();

    if (!empty($instances) && isset($instances[0])) {
@@ -98,6 +165,7 @@ class InstancesController extends ControllerBase {
      }
    }

    return null;
    return NULL;
  }

}
+1 −1

File changed.

Contains only whitespace changes.

+1 −1

File changed.

Contains only whitespace changes.

+1 −1

File changed.

Contains only whitespace changes.

+1 −1

File changed.

Contains only whitespace changes.

+1 −1

File changed.

Contains only whitespace changes.

+1 −1

File changed.

Contains only whitespace changes.

+1 −1

File changed.

Contains only whitespace changes.