Skip to content
Snippets Groups Projects
Commit 5e25d038 authored by catch's avatar catch
Browse files

Initial WIP, doesn't work yet.

parent b56d4275
No related branches found
No related tags found
No related merge requests found
Showing
with 244 additions and 0 deletions
<?php
declare(strict_types=1);
use Drupal\lms_certificate\Hook\LmsCertificateHooks;
/**
* Implements hook_install().
*/
function lms_certificate_install() {
$group_entity_type = \Drupal::service('entity_type.manager')->getDefinition('group');
$definition = \Drupal::service(LmsCertificateHooks::class)->entityFieldStorageInfo($group_entity_type)['certificate'];
\Drupal::entityDefinitionUpdateManager()
->installFieldStorageDefinition('certificate', 'group', 'lms_certificate', $definition);
}
<?php
declare(strict_types=1);
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\lms_certificate\Hook\LmsCertificateHooks;
/**
* Implements hook_entity_field_storage_info()
*/
#[LegacyHook]
function lms_certificate_entity_field_storage_info(EntityTypeInterface $entity_type): array {
return \Drupal::service(LmsCertificateHooks::class)->entityfieldStorageInfo($entity_type);
}
/**
* Implements hook_entity_field_bundle_info()
*/
#[LegacyHook]
function lms_certificate_entity_bundle_field_info(EntityTypeInterface $entity_type, string $bundle): array {
return \Drupal::service(LmsCertificateHooks::class)->entityBundleFieldInfo($entity_type, $bundle);
}
services:
Drupal\lms_certificate\Hook\LmsCertificateHooks:
class: Drupal\lms_certificate\Hook\LmsCertificateHooks
autowire: true
type: module
name: LMS Certificate FillPDF
description: Allows FillPDF entities to be used as LMS certificates.
package: LMS
dependencies:
- drupal:lms_certificate
core_version_requirement: ^10.3 || ^11
<?php
declare(strict_types=1);
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\lms_certificate_fillpdf\Hook\LmsCertificateFillPdfHooks;
/**
* Implements hook_entity_field_bundle_info_alter()
*/
#[LegacyHook]
function lms_certificate_entity_bundle_field_info_alter(array &$fields, EntityTypeInterface $entity_type, string $bundle) {
\Drupal::service(LmsCertificateFillPdfHooks::class)->entityBundleFieldInfoAlter($fields, $entity_type, $bundle);
}
lms_certificate.certificate:
path: 'group/{group}/certificate'
defaults:
_controller: '\Drupal\lms_certificate_fillpdf\Controller\CertificateController::getCertificate'
_title: 'Certificate'
requirements:
_custom_access: '\Drupal\lms_certificate_fillpdf\Controller\CertificateController::access'
services:
Drupal\lms_certificate_fillpdf\Hook\LmsCertificateFillPdfHooks:
class: Drupal\lms_certificate_fillpdf\Hook\LmsCertificateFillPdfHooks
autowire: true
<?php
declare(strict_types=1);
namespace Drupal\lms_certificate;
use Drupal\Core\Field\BaseFieldDefinition;
/**
* Provides a field definition class for bundle fields.
*
* Copied from https://git.drupalcode.org/project/entity/-/blob/8.x-1.x/src/BundleFieldDefinition.php?ref_type=heads
*
* @see https://www.drupal.org/node/2280639
*
*/
class BundleFieldDefinition extends BaseFieldDefinition {
/**
* {@inheritdoc}
*/
public function isBaseField(): bool {
return FALSE;
}
}
<?php
declare(strict_types=1);
namespace Drupal\lms_certificate_fillpdf\Controller;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Controller\ControllerResolverInterface;
use Drupal\group\Entity\GroupInterface;
use Drupal\fillpdf\Controller\HandlePdfController;
use Symfony\Component\DependencyInjection\ContainerInterface;
class CertificateController extends ControllerBase {
public function __construct(protected ControllerResolverInterface $controllerResolver) {}
public static function create(ContainerInterface $container) {
return new static(
$container->get('controller_resolver')
);
}
public function getCertificate(GroupInterface $group) {
$controller_resolver = \Drupal::service('controller_resolver');
$controller = $controller_resolver->getControllerFromDefinition(HandlePdfController::class . '::populatePdf');
return $controller();
}
public function access(GroupInterface $group) {
return AccessResult::allowed();
}
}
<?php
declare(strict_types=1);
namespace Drupal\lms_certificate_fillpdf\Hook;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\FieldDefinition;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\group\Entity\Group;
use Drupal\lms_certificate\BundleFieldDefinition;
class LmsCertificateFillPdfHooks {
#[Hook('entity_bundle_field_info_alter')]
public function entityBundleFieldInfoAlter(array &$fields, EntityTypeInterface $entity_type, string $bundle) {
if ($bundle === 'lms_course' && $entity_type->id() === 'group') {
$entity_type_ids = $fields['certificate']->getSetting('entity_type_ids');
$entity_type_ids[] = 'fillpdf_form';
$fields['certificate']->setSetting('entity_type_ids', $entity_type_ids);
}
}
}
<?php
declare(strict_types=1);
namespace Drupal\lms_certificate;
use Drupal\Core\Field\BaseFieldDefinition;
/**
* Provides a field definition class for bundle fields.
*
* Copied from https://git.drupalcode.org/project/entity/-/blob/8.x-1.x/src/BundleFieldDefinition.php?ref_type=heads
*
* @see https://www.drupal.org/node/2280639
*
*/
class BundleFieldDefinition extends BaseFieldDefinition {
/**
* {@inheritdoc}
*/
public function isBaseField(): bool {
return FALSE;
}
}
<?php
declare(strict_types=1);
namespace Drupal\lms_certificate\Hook;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\FieldDefinition;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\group\Entity\Group;
use Drupal\lms_certificate\BundleFieldDefinition;
class LmsCertificateHooks {
#[Hook('entity_field_storage_info')]
public function entityFieldStorageInfo(EntityTypeInterface $entity_type) {
$fields = [];
if ($entity_type->id() === 'group') {
$fields['certificate'] = BundleFieldDefinition::create('dynamic_entity_reference')
->setTargetEntityTypeId('group')
->setLabel(new TranslatableMarkup('Certificate'))
->setName('membership_group')
->setDescription(new TranslatableMarkup('Reference to an entity providing a certificate'))
->setSetting('target_type', 'group')
->setSetting('target_bundles', [
'lms_course' => 'lms_course',
]);
}
return $fields;
}
#[Hook('entity_bundle_field_info')]
public function entityBundleFieldInfo(EntityTypeInterface $entity_type, string $bundle) {
$fields = [];
if ($bundle === 'lms_course' && $entity_type->id() === 'group') {
$fields['certificate'] = BundleFieldDefinition::create('dynamic_entity_reference')
->setTargetEntityTypeId('group')
->setDisplayConfigurable('form', TRUE)
->setLabel(new TranslatableMarkup('Certificate'))
->setDescription(new TranslatableMarkup('Reference to an entity providing a certificate'))
->setSetting('exclude_entity_types', FALSE)
->setSetting('entity_type_ids', [])
->setSetting('target_type', 'group')
->setSetting('target_bundles', [
'lms_course' => 'lms_course',
]);
}
return $fields;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment