page_tester-1.0.x-dev/src/FieldDefinitionService.php

src/FieldDefinitionService.php
<?php

namespace Drupal\page_tester;

use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\field\FieldConfigInterface;
use Drupal\node\Entity\Node;

/**
 * Orgchart service.
 */
class FieldDefinitionService {

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

  /**
   * The file system.
   *
   * @var \Drupal\Core\File\FileSystemInterface
   */
  protected $fileSystem;

  /**
   * The field definition interface.
   *
   * @var \Drupal\Core\Entity\EntityFieldManagerInterface
   */
  protected $fieldManager;

  /**
   * DataService constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   The database connection.
   * @param \Drupal\Core\File\FileSystemInterface $file_system
   *   The file system.
   * @param \Drupal\Core\Entity\EntityFieldManagerInterface $field_manager
   *   The module extension list.
   */
  public function __construct(
    EntityTypeManagerInterface $entityTypeManager,
    FileSystemInterface $file_system,
    EntityFieldManagerInterface $field_manager,
  ) {
    $this->entityTypeManager = $entityTypeManager;
    $this->fileSystem = $file_system;
    $this->fieldManager = $field_manager;
  }

  /**
   * Create a capitalizes name from machine name.
   *
   * @param string $machine_name
   *   The machine name.
   *
   * @return string
   *   A string space delimited with first letters capitalized.
   */
  public function createName($machine_name) {
    return ucfirst(implode(" ", explode("_", $machine_name)));
  }

  /**
   * Returns entity type fields that are instances of FieldConfigInterface.
   *
   * @param string $entityType
   *   The entity type machine name.
   * @param string $bundle
   *   The entity type bundle name.
   *
   * @return array
   *   An array of filtered fields.
   */
  public function entityTypeFields($entityType, $bundle = NULL) {
    $fields = [];

    if (!empty($entityType)) {
      $types = $this->fieldManager->getFieldMap();
      foreach ($types as $type => $definitions) {
        if ($type == $entityType) {
          if ($bundle) {
            $fields = array_filter(
              $this->fieldManager->getFieldDefinitions($entityType, $bundle),
              function ($field_definition) {
                return $field_definition instanceof FieldConfigInterface;
              }
            );
          } else {
            $fields = $definitions;
          }
        }
      }
    }

    return $fields;
  }

  /**
   * Returns all vocabulary fields by vocabulary.
   *
   * @param string $type
   *   The vocabulary machine name.
   *
   * @return array $options
   *   An options array of fields.
   */
  public function getVocabularyFields($type) {
    $options = [];
    $fields = $this->entityTypeFields('taxonomy_term', $type);
    foreach ($fields as $fieldID => $field) {
      $options[$fieldID] = $fieldID;
    }
    return $options;
  }

  /**
   * Returns all content type fields by content type.
   *
   * @param string $content_type
   *   The vocabulary machine name.
   *
   * @return array $options
   *   An options array of fields.
   */
  public function getContentTypeLinks($content_type) {
    $options = [];
    $content_types = \Drupal::entityTypeManager()
      ->getStorage('node_type')
      ->loadMultiple();
    foreach ($content_types as $entityTypeId => $entityType) {
      if($entityTypeId == $content_type) {
        /** @var string $entityTypeId */
        $nids = \Drupal::entityQuery('node')
          ->condition('type', $entityTypeId)
          ->accessCheck(FALSE) // Be mindful of access control when querying
          ->execute();
        $nodes = Node::loadMultiple($nids);
        if ($nodes) {
          foreach ($nodes as $node) {
            $options[$node->id()] = $node->toUrl()->toString();
          }
        }
      }
    }
    return $options;
  }

  /**
   * Returns all content type fields by content type.
   *
   * @param string $menu_id
   *   The vocabulary machine name.
   *
   * @return array $options
   *   An options array of fields.
   */
  public function getMenuLinks($menu_id) {
    $options = [];
    $menu_storage = \Drupal::entityTypeManager()->getStorage('menu');
    $menus = $menu_storage->loadMultiple();
    foreach ($menus as $menuId => $menu) {
      if ($menuId == $menu_id) {
        $storage = \Drupal::entityTypeManager()->getStorage('menu_link_content');
        $menu_links = $storage->loadByProperties(['menu_name' => $menuId]);
        foreach ($menu_links as $mlid => $menu_link) {
          /** @var Drupal\Core\Url $menu_link */
          if($menu_link->isEnabled()) {
            $url_object = $menu_link->getUrlObject();
            $alias = $url_object->toString();
            if ($alias && !in_array($alias, $options)) {
              $options[$mlid] = $alias;
            }
          }
        }
      }
    }

    return $options;
  }

  /**
   * Returns all vocabulary fields on site.
   *
   * @return array $options
   *   An options array of fields.
   */
  public function getAllVocabularyFields() {
    $options = [];
    $taxonomies = \Drupal::entityTypeManager()
      ->getStorage('taxonomy_vocabulary')
      ->loadMultiple();
    foreach ($taxonomies as $entityTypeId => $entityType) {
      /** @var string $entityTypeId */
      $fields = $this->getVocabularyFields($entityTypeId);
      if(count($fields) > 0) {
        foreach($fields as $field) {
          $options[$field] = $field;
        }
      }
    }
    return $options;
  }

  /**
   * Returns all content type links on site.
   *
   * @return array $options
   *   An options array of fields.
   */
  public function getAllContentTypeLinks() {
    $options = [];
    $content_types = \Drupal::entityTypeManager()
      ->getStorage('node_type')
      ->loadMultiple();
    foreach ($content_types as $entityTypeId => $entityType) {
      /** @var string $entityTypeId */
      $nids = \Drupal::entityQuery('node')
        ->condition('type', $entityTypeId)
        ->accessCheck(FALSE) // Be mindful of access control when querying
        ->execute();
      $nodes = Node::loadMultiple($nids);
      if ($nodes) {
        foreach ($nodes as $node) {
          if($node->isPublished()) {
            $options[$node->id()] = $node->toUrl()->toString();
          }
        }
      }
    }
    return $options;
  }

  /**
   * Returns all menu links on site.
   *
   * @return array $options
   *   An options array of fields.
   */
  public function getAllMenuLinks() {
    $options = [];
    $menu_storage = \Drupal::entityTypeManager()->getStorage('menu');
    $menus = $menu_storage->loadMultiple();
    foreach ($menus as $menuId => $menu) {
      $storage = \Drupal::entityTypeManager()->getStorage('menu_link_content');
      $menu_links = $storage->loadByProperties(['menu_name' => $menuId]);
      foreach ($menu_links as $mlid => $menu_link) {
        /** @var Drupal\Core\Url $menu_link */
        if ($menu_link->isEnabled()) {
          $url_object = $menu_link->getUrlObject();
          $alias = $url_object->toString();
          if ($alias && !in_array($alias, $options)) {
            $options[$mlid] = $alias;
          }
        }
      }
    }
    return $options;
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc