cms_content_sync-3.0.x-dev/modules/cms_content_sync_views/cms_content_sync_views.module

modules/cms_content_sync_views/cms_content_sync_views.module
<?php

/**
 * @file
 * Module file for cms_content_sync_views.
 */

use Drupal\cms_content_sync\Entity\EntityStatus;
use Drupal\cms_content_sync\Plugin\Type\EntityHandlerPluginManager;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;

/**
 * Implements hook_entity_base_field_info().
 */
function cms_content_sync_views_entity_base_field_info(EntityTypeInterface $entity_type) {
  $fields = [];

  // Add the dynamic entity reference field to the entity status entity type.
  if ('cms_content_sync_entity_status' === $entity_type->id()) {
    $fields['entity'] = BaseFieldDefinition::create('dynamic_entity_reference')
      ->setLabel(t('Entity'))
      ->setDescription(t('Reference to the entity.'))
      ->setCardinality(1)
      ->setReadOnly(TRUE);
  }

  return $fields;
}

/**
 * Sets the value for DER field that is attached to the status entity.
 *
 * @param null|EntityInterface $reference
 *   The entity to be referenced.
 *
 * @throws \Drupal\Core\Entity\EntityStorageException
 */
function _cms_content_sync_views_set_dynamic_entity_reference_field(EntityInterface $entity, $reference = NULL) {
  // Set the referenced entity for the entity status entity.
  if (is_null($entity->get('entity')->target_id)) {
    $entity_type = $entity->get('entity_type')->value;
    $uuid = $entity->get('entity_uuid')->value;

    /**
     * @var \Drupal\Core\Entity\EntityInterface $synced_entity
     */
    if (empty($reference)) {
      $synced_entity = Drupal::service('entity.repository')->loadEntityByUuid($entity_type, $uuid);
    }
    else {
      $synced_entity = $reference;
    }

    if (EntityHandlerPluginManager::isEntityTypeConfiguration($entity_type)) {
      return;
    }

    if (!is_null($synced_entity)) {
      $entity->set('entity', $synced_entity);

      // Save new reference.
      $entity->save();
    }
  }
}

/**
 * Implements hook_entity_create().
 *
 * Required for the push.
 */
function cms_content_sync_views_entity_create(EntityInterface $entity) {
  if ($entity instanceof EntityStatus) {
    _cms_content_sync_views_set_dynamic_entity_reference_field($entity);
  }
}

/**
 * Implements hook_entity_insert().
 *
 * Required for the pull.
 */
function cms_content_sync_views_entity_insert(EntityInterface $entity) {
  if ($entity instanceof EntityStatus) {
    _cms_content_sync_views_set_dynamic_entity_reference_field($entity);
  }
}

/**
 * Implements hook_entity_update().
 *
 * Fallback.
 */
function cms_content_sync_views_entity_update(EntityInterface $entity) {
  if ($entity instanceof EntityStatus) {
    _cms_content_sync_views_set_dynamic_entity_reference_field($entity);
  }
}

/**
 * Implements hook_views_data_alter().
 */
function cms_content_sync_views_views_data_alter(array &$data) {
  $allowed_entity_types = [
    'node',
    'media',
    'redirect',
    'block_content',
    'taxonomy_term',
    'menu_link_content',
    'paragraphs_library_item',
  ];

  foreach ($allowed_entity_types as $allowed_entity_type) {
    $data[$allowed_entity_type]['cms_content_sync_sync_state'] = [
      'title' => t('Sync State'),
      'field' => [
        'title' => t('Sync State'),
        'help' => t('Displays the Content Sync Sync State.'),
        'id' => 'cms_content_sync_sync_state',
      ],
      'filter' => [
        'title' => t('Content synchronization'),
        'help' => t('Filter if a entity has been pulled or pushed with Content Sync.'),
        'id' => 'cms_content_sync_sync_state_filter',
      ],
    ];
  }

  $data['cms_content_sync_entity_status']['cms_content_sync_rendered_flags'] = [
    'title' => t('Rendered Flags'),
    'field' => [
      'title' => t('Rendered Flags'),
      'help' => t('The status entity flags made human readable.'),
      'id' => 'cms_content_sync_rendered_flags',
    ],
    'filter' => [
      'title' => t('Rendered Flags'),
      'help' => t('Creates a human readable flags filter.'),
      'id' => 'cms_content_sync_flags_filter',
    ],
  ];

  $data['cms_content_sync_entity_status']['entity_type'] = [
    'title' => t('Entity Type'),
    'field' => [
      'title' => t('Entity Type label'),
      'help' => t('The entity type label'),
      'id' => 'cms_content_sync_entity_type_label',
    ],
    'filter' => [
      'title' => t('Entity Type'),
      'help' => t('Creates a human readable entity type filter.'),
      'id' => 'cms_content_sync_entity_type_filter',
    ],
  ];

  $data['cms_content_sync_entity_status']['pool'] = [
    'title' => t('Pool label'),
    'field' => [
      'title' => t('Pool label'),
      'help' => t('The pool label'),
      'id' => 'cms_content_sync_pool_label',
    ],
    'filter' => [
      'title' => t('Pool'),
      'help' => t('Creates a human readable pool filter.'),
      'id' => 'cms_content_sync_pool_filter',
    ],
  ];

  $data['cms_content_sync_entity_status']['flow'] = [
    'title' => t('Flow label'),
    'field' => [
      'title' => t('Flow label'),
      'help' => t('The flow label'),
      'id' => 'cms_content_sync_flow_label',
    ],
    'filter' => [
      'title' => t('Flow'),
      'help' => t('Creates a human readable flow filter.'),
      'id' => 'cms_content_sync_flow_filter',
    ],
  ];

  $data['cms_content_sync_entity_status']['parent_entity'] = [
    'title' => t('Parent entity'),
    'field' => [
      'title' => t('Parent entity'),
      'help' => t('The parent entity'),
      'id' => 'cms_content_sync_parent_entity',
    ],
  ];
}

/**
 * Implements hook_theme().
 */
function cms_content_sync_views_theme() {
  return [
    'sync_status' => [
      'variables' => ['messages' => NULL, 'update_status' => NULL],
    ],
    'rendered_flags' => [
      'variables' => ['messages' => NULL],
    ],
  ];
}

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

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