content_entity_builder-8.x-1.x-dev/content_entity_builder.module

content_entity_builder.module
<?php

/**
 * @file
 * Provides content entity by config.
 */

use Drupal\Core\Entity\ContentEntityType;
use Drupal\Core\Config\Entity\ConfigEntityType;

/**
 * Implements hook_entity_type_build().
 */
function content_entity_builder_entity_type_build(array &$entity_types) {
  // Check for content_type config entity.
  if (!empty($entity_types['content_type'])) {
    $content_type = $entity_types['content_type'];
    // unset($entity_types['content_type']);
    // Get the existing content types.
    // $content_types = ContentType::loadMultiple();
    $content_types = \Drupal::entityTypeManager()->createHandlerInstance(
      $content_type->getHandlerClass('storage'),
      $content_type
    )->loadMultiple();

    // Base definitions for the entity type.
    $base_definition = [
      'handlers' => [
        'view_builder' => 'Drupal\Core\Entity\EntityViewBuilder',
        'form' => [
          'default' => 'Drupal\content_entity_builder\Form\ContentForm',
          'add' => 'Drupal\content_entity_builder\Form\ContentForm',
          'edit' => 'Drupal\content_entity_builder\Form\ContentForm',
          'delete' => 'Drupal\Core\Entity\ContentEntityDeleteForm',
        ],
        'list_builder' => 'Drupal\content_entity_builder\ContentEntityBuilderListBuilder',
        'access' => 'Drupal\content_entity_builder\ContentEntityBuilderAccessControlHandler',
        'views_data' => 'Drupal\views\EntityViewsData',
        'storage_schema' => 'Drupal\content_entity_builder\ContentEntityBuilderStorageSchema',
      ],
      //'translatable' => FALSE,
      'provider' => 'content_entity_builder',
      //'class' => 'Drupal\content_entity_builder\Entity\Content',
      'group' => 'content',
      'group_label' => t('Content'),
    ];
	
    // Base definitions for the entity bundle.
    $base_bundle_definition = [
      'handlers' => [
        'form' => [
          'default' => 'Drupal\content_entity_builder\Form\ContentTypeBundleForm',
          'add' => 'Drupal\content_entity_builder\Form\ContentTypeBundleForm',
          'edit' => 'Drupal\content_entity_builder\Form\ContentTypeBundleForm',
          'delete' => 'Drupal\content_entity_builder\Form\ContentTypeBundleDeleteForm',
        ],
        'list_builder' => 'Drupal\content_entity_builder\ContentTypeBundleListBuilder',
      ],
      'provider' => 'content_entity_builder',
      'class' => 'Drupal\content_entity_builder\Entity\ContentTypeBundle',
      'group' => 'configuration',
      'group_label' => t('Configuration'),
      'entity_keys' => [
        'id' => 'id',
        'label' => 'label',
      ],
	  'config_export' => ['id', 'label', 'description'],
      'admin_permission' => 'administer content entity bundles',	  
    ];	

    if (!empty($content_types)) {
      // Add custom particular definitions for each entity.
      foreach ($content_types as $type) {
        $content_type_id = $type->id();
        $type = \Drupal::entityTypeManager()->createHandlerInstance(
            $content_type->getHandlerClass('storage'),
            $content_type
          )->load($content_type_id);

        // Skip it if it is not applied update.
        if (!$type->isApplied()) {
          continue;
        }

        // Definitions for the entity type.
        $paths = $type->getEntityPaths();
        $path_view = !empty($paths['view']) ? $paths['view'] : "/$content_type_id/{" . $content_type_id . "}";
        $path_add = !empty($paths['add']) ? $paths['add'] : "/$content_type_id/add";
        $path_edit = !empty($paths['edit']) ? $paths['edit'] : "/$content_type_id/{" . $content_type_id . "}/edit";
        $path_delete = !empty($paths['delete']) ? $paths['delete'] : "/$content_type_id/{" . $content_type_id . "}/delete";
        $definition = [
          'id' => $content_type_id,
          'label' => $type->label(),
          'base_table' => $content_type_id,
          'links' => [
            'canonical' => $path_view,
            'add-form' => $path_add,
            'edit-form' => $path_edit,
            'delete-form' => $path_delete,
            'collection' => '/admin/structure/content-types/manage/{' . $content_type_id . '}/list',
          ],
          'field_ui_base_route' => 'entity.' . $type->id() . '.admin_form',
        ];
        $keys = array_filter($type->getEntityKeys());
        if (empty($keys)) {
          $keys = [
            'id' => 'id',
            'uuid' => 'uuid',
          ];
        }
        $definition['entity_keys'] = $keys;
        $definition['config_dependencies'] = [
          'config' => ["content_entity_builder.content_type.$content_type_id"],
        ];
		//support bundles if entity type has
		$mode = $type->getMode() ?? "basic";
		if($mode === "basic" || $mode === "basic_plus"){
            $definition['class'] = 'Drupal\content_entity_builder\Entity\Content';
			$definition['translatable'] = FALSE;
        }			
		if($mode === "basic_plus" || $mode === "advanced" || $mode === "full"){
			$definition['bundle_label'] = t('@entity_type type', ['@entity_type' => $type->label()]);
            $definition['bundle_entity_type'] = $content_type_id . '_type';
            $definition['field_ui_base_route'] = 'entity.' . $content_type_id . '_type.edit_form';
            $definition['permission_granularity'] = 'bundle';			
		}
		if($mode === "advanced" || $mode === "full"){
		  $definition['class'] = 'Drupal\content_entity_builder\Entity\AdvancedContent';			
          $definition['translatable'] = TRUE;
          $definition['data_table'] = $content_type_id . "_field_data";
		}
		if($mode === "full"){
		  $definition['class'] = 'Drupal\content_entity_builder\Entity\FullContent';
          $definition['revision_table'] = $content_type_id . "__revision";
          $definition['revision_data_table'] = $content_type_id . "_field_revision";
          $definition['show_revision_ui'] = TRUE;		  
          $definition['revision_metadata_keys'] = [
            'revision_user' => 'revision_uid',
            'revision_created' => 'revision_timestamp',
            'revision_log_message' => 'revision_log',
          ];  
		}		
		
        // Merge the definitions.
        $definition = array_merge($definition, $base_definition);
        // Add the new content entity to the entity types.
        $entity_types[$definition['id']] = new ContentEntityType($definition);
		
		
		if($mode === "basic_plus" || $mode === "advanced" || $mode === "full"){
		
          $bundle_definition = [
            'id' => $content_type_id . '_type',
            'label' => t('@entity_type type', ['@entity_type' => $type->label()]),
            'bundle_of' => $content_type_id,
            'config_prefix' => $content_type_id,
            'links' => [
              'add-form' => '/admin/structure/content-types/manage/' . $content_type_id . '/bundles/add',
              'edit-form' => '/admin/structure/content-types/manage/' . $content_type_id . '/bundles/{' . $content_type_id . '_type}/edit',
              'delete-form' => '/admin/structure/content-types/manage/' . $content_type_id . '/bundles/{' . $content_type_id . '_type}/delete',
              'collection' => '/admin/structure/content-types/manage/' . $content_type_id . '/bundles',
            ],
          ];

          $bundle_definition = array_merge($bundle_definition, $base_bundle_definition);
          $entity_types[$bundle_definition['id']] = new ConfigEntityType($bundle_definition);	
        }		
      }
    }
  }

}


/**
 * Implements hook_file_download().
 */
function content_entity_builder_file_download($uri) {
  //$scheme = file_uri_scheme($uri);
  $scheme = \Drupal::service('stream_wrapper_manager')->getScheme($uri);
  $target = \Drupal::service('stream_wrapper_manager')->getTarget($uri);
  $pos = strpos($target, '.tar.gz');
  if ($scheme == 'temporary' && $pos) {
    if (\Drupal::currentUser()->hasPermission('administer content entity types')) {
	  $filename = $target;
      $disposition = 'attachment; filename="' . $filename . '"';
      return [
        'Content-disposition' => $disposition,
      ];
    }
    return -1;
  }
}

/**
 * Implements hook_theme().
 */
function content_entity_builder_theme($existing, $type, $theme, $path) {
  $hook_themes = [];

  if ($type == 'module') {
    $content_types = \Drupal::entityTypeManager()->getStorage('content_type')->loadMultiple();
    foreach ($content_types as $content_type) {
      $hook_themes[$content_type->id()] = [
        'base hook' => 'content-entity',
        'render element' => 'content',
        'template' => 'content-entity'
      ];
    }
  }
  return $hook_themes;
}

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

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