Repository URL to install this package:
|
Version:
1.3.0 ▾
|
<?php
namespace Drupal\custom_forms\Entity;
use Drupal\Core\Entity\Annotation\ContentEntityType;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\TypedData\Exception\MissingDataException;
use Drupal\custom_forms\CustomFormInterface;
use Drupal\custom_forms\Plugin\CustomForms\SubmissionHandler\CustomFormsSubmissionHandlerInterface;
use Drupal\user\EntityOwnerTrait;
/**
* Class CustomFormSubmissionHandler
*
* @package Drupal\custom_forms\Entity
*
* @ContentEntityType(
* id = "custom_form_submission_handler",
* label = @Translation("Custom Form Submission Handler"),
* label_collection = @Translation("Custom Form Submissions Handler"),
* label_singular = @Translation("Custom Form Submission Handler"),
* label_plural = @Translation("Custom Form Submission Handlers"),
* label_count = @PluralTranslation(
* singular = "@count custom form submission",
* plural = "@count custom form submission handlers"
* ),
* handlers = {
* "access" = "Drupal\custom_forms\CustomFormHandlerAccessControlHandler",
* "form" = {
* "add" = "Drupal\custom_forms\Form\CustomFormHandlerForm",
* "edit" = "Drupal\custom_forms\Form\CustomFormHandlerForm",
* "delete" = "Drupal\custom_forms\Form\CustomFormHandlerDeleteForm"
* },
* "route_provider" = {
* "html" = "Drupal\custom_forms\Routing\CustomFormSubmissionHandlerHtmlRouteProvider",
* },
* },
* fieldable = FALSE,
* admin_permission = "administer custom forms",
* base_table = "custom_forms_submission_handlers",
* show_revision_ui = FALSE,
* translatable = FALSE,
* entity_keys = {
* "id" = "id",
* "uid" = "uid",
* "owner" = "uid",
* "label" = "label",
* },
* links = {
* "add-form" = "/admin/custom_forms/{custom_form}/handlers/add",
* "edit-form" = "/admin/custom_forms/{custom_form}/handlers/{custom_form_submission_handler}/settings",
* "delete-form" = "/admin/custom_forms/{custom_form}/handlers/{custom_form_submission_handler}/delete",
* "collection" = "/admin/custom_forms/{custom_form}/handlers",
* }
* )
*/
class CustomFormSubmissionHandler extends ContentEntityBase implements CustomFormSubmissionHandlerInterface {
use EntityChangedTrait;
use EntityOwnerTrait;
use StringTranslationTrait;
/**
* The factory in charge of CRUD operations on custom form items.
*
* @var \Drupal\custom_forms\CustomFormItemFactory $itemFactory
*/
protected $itemFactory;
/**
* {@inheritdoc}
*/
public function __construct(array $values, $entity_type, $bundle = FALSE, $translations = []) {
parent::__construct($values, $entity_type, $bundle, $translations);
$this->itemFactory = \Drupal::service('custom_forms.factory.item');
}
/**
* {@inheritdoc}
*/
public function getCustomForm(): CustomFormInterface {
return $this->get('form')->entity;
}
/**
* {@inheritdoc}
*/
public function getPluginId() : string {
return $this->get('plugin')->value;
}
/**
* {@inheritdoc}
*/
public function getPluginDefinition() : ?array {
$plugin_id = $this->get('plugin')->value;
$handlerPluginManager = \Drupal::service('plugin.manager.custom_forms_submission_handlers');
return $handlerPluginManager->getDefinition($plugin_id);
}
/**
* {@inheritdoc}
*/
public function getPluginSettings() : array {
try {
if ($this->get('settings')->first()) {
return $this->get('settings')->first()->getValue() ?? [];
}
else {
return [];
}
} catch (MissingDataException $e) {
return [];
}
}
/**
* {@inheritdoc}
*/
public function getPlugin() : object {
/** @var \Drupal\custom_forms\CustomFormsSubmissionHandlerManager $handlerPluginManager */
$handlerPluginManager = \Drupal::service('plugin.manager.custom_forms_submission_handlers');
return $handlerPluginManager->createInstance($this->getPluginId(), $this->getPluginSettings());
}
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
// Add the owner field
$fields += static::ownerBaseFieldDefinitions($entity_type);
$fields['uid']
->setLabel(t('Authored by'))
->setDescription(t('The user ID of author of the handler.'))
->setRevisionable(TRUE)
->setSetting('target_type', 'user')
->setSetting('handler', 'default')
->setTranslatable(TRUE)
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['form'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Custom Form'))
->setDescription(t('The custom form which the handler belongs to.'))
->setCardinality(1)
->setRequired(TRUE)
->setSetting('target_type', 'custom_form')
->setSetting('handler', 'default')
->setDisplayConfigurable('form', FALSE)
->setDisplayConfigurable('view', FALSE);
$fields['label'] = BaseFieldDefinition::create('string')
->setTranslatable(TRUE)
->setRevisionable(TRUE)
->setLabel(t('Label'))
->setDescription(t('The label of the handler.'))
->setRequired(TRUE)
->setSetting('max_length', 255)
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -5,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'string',
'weight' => -5,
])
->setDisplayConfigurable('view', TRUE);
$fields['plugin'] = BaseFieldDefinition::create('string')
->setLabel(t('Handler plugin'))
->setDescription(t('The id for the plugin used by the handler.'))
->setRequired(TRUE)
->setSetting('max_length', 255)
->setDisplayConfigurable('form', FALSE)
->setDisplayConfigurable('view', FALSE);
$fields['settings'] = BaseFieldDefinition::create('map')
->setLabel(t('Settings'))
->setDescription(t('A serialized array of handler settings.'))
->setDefaultValue([]);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Created'))
->setDescription(t('The time that the handler was created.'));
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time that the handler was last edited.'));
return $fields;
}
}