class CreateTemplateForm in GatherContent 8.5
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\gathercontent_upload_ui\Form\CreateTemplateForm
Expanded class hierarchy of CreateTemplateForm
1 string reference to 'CreateTemplateForm'
- gathercontent_upload_ui.routing.yml in gathercontent_upload_ui/
gathercontent_upload_ui.routing.yml - gathercontent_upload_ui/gathercontent_upload_ui.routing.yml
File
- gathercontent_upload_ui/
src/ Form/ CreateTemplateForm.php, line 13
Namespace
Drupal\gathercontent_upload_ui\FormView source
class CreateTemplateForm extends FormBase {
/**
* Entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The entity bundle info.
*
* @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
*/
protected $entityTypeBundleInfo;
/**
* Mapping creator.
*
* @var \Drupal\gathercontent_upload\Export\MappingCreator
*/
protected $mappingCreator;
/**
* MappingCreator constructor.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entityTypeBundleInfo
* @param \Drupal\gathercontent_upload\Export\MappingCreator $mappingCreator
*/
public function __construct(EntityTypeManagerInterface $entityTypeManager, EntityTypeBundleInfoInterface $entityTypeBundleInfo, MappingCreator $mappingCreator) {
$this->entityTypeManager = $entityTypeManager;
$this->entityTypeBundleInfo = $entityTypeBundleInfo;
$this->mappingCreator = $mappingCreator;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('entity_type.bundle.info'), $container
->get('gathercontent_upload.mapping_creator'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'gathercontent_create_template_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form['gathercontent']['entity_type'] = [
'#type' => 'select',
'#title' => $this
->t('Drupal entity type'),
'#options' => $this
->getEntityTypes(),
'#required' => TRUE,
'#wrapper_attributes' => [
'class' => [
'inline-label',
],
],
'#ajax' => [
'callback' => '::getContentTypes',
'wrapper' => 'content-type-select',
'method' => 'replace',
'effect' => 'fade',
],
'#default_value' => $form_state
->getValue('entity_type'),
];
$form['gathercontent']['content_type'] = [
'#type' => 'select',
'#title' => $this
->t('Drupal bundle type'),
'#options' => $form_state
->getValue('entity_type') ? $this
->getBundles($form_state
->getValue('entity_type')) : [],
'#required' => TRUE,
'#wrapper_attributes' => [
'class' => [
'inline-label',
],
],
'#prefix' => '<div id="content-type-select">',
'#suffix' => '</div>',
];
$form['gathercontent']['project_id'] = [
'#type' => 'select',
'#title' => $this
->t('Project ID'),
'#options' => $this
->getProjects(),
'#required' => TRUE,
'#wrapper_attributes' => [
'class' => [
'inline-label',
],
],
'#default_value' => $form_state
->getValue('project_id'),
];
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Create'),
'#button_type' => 'primary',
'#weight' => 10,
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->mappingCreator
->generateMapping($form_state
->getValue('entity_type'), $form_state
->getValue('content_type'), $form_state
->getValue('project_id'));
$this
->messenger()
->addMessage('Mapping created successfully');
}
/**
* Get list of bundle types.
*
* @param string $entityType
* Entity type ID.
*
* @return array
* Assoc array of bundle types.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
public function getBundles(string $entityType) : array {
$mappingStorage = $this->entityTypeManager
->getStorage('gathercontent_mapping');
$bundleTypes = $this->entityTypeBundleInfo
->getBundleInfo($entityType);
$response = [];
foreach ($bundleTypes as $key => $value) {
$mapping = $mappingStorage
->loadByProperties([
'entity_type' => $entityType,
'content_type' => $key,
]);
if ($mapping) {
continue;
}
$response[$key] = $value['label'];
}
return $response;
}
/**
* Get list of entity types.
*
* @return array
* Assoc array of entity types.
*/
public function getEntityTypes() {
$entityTypes = $this->entityTypeManager
->getDefinitions();
$unsupportedTypes = [
'user',
'file',
'menu_link_content',
];
$response = [];
foreach ($entityTypes as $key => $value) {
if ($value) {
$class = $value
->getOriginalClass();
if (in_array(FieldableEntityInterface::class, class_implements($class)) && !in_array($key, $unsupportedTypes)) {
$label = (string) $value
->getLabel();
$response[$key] = $label;
}
}
}
return $response;
}
/**
* Ajax callback for mapping multistep form.
*
* @param array $form
* @param \Drupal\Core\Form\FormStateInterface $form_state
*
* @return array
* Array of form elements.
*/
public function getContentTypes(array &$form, FormStateInterface $form_state) {
$form_state
->setRebuild(TRUE);
return $form['gathercontent']['content_type'];
}
/**
* Returns all projects for given account.
*
* @return array
*/
public function getProjects() {
return $this->mappingCreator
->getProjects();
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CreateTemplateForm:: |
protected | property | The entity bundle info. | |
CreateTemplateForm:: |
protected | property | Entity type manager. | |
CreateTemplateForm:: |
protected | property | Mapping creator. | |
CreateTemplateForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
CreateTemplateForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
CreateTemplateForm:: |
public | function | Get list of bundle types. | |
CreateTemplateForm:: |
public | function | Ajax callback for mapping multistep form. | |
CreateTemplateForm:: |
public | function | Get list of entity types. | |
CreateTemplateForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
CreateTemplateForm:: |
public | function | Returns all projects for given account. | |
CreateTemplateForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
CreateTemplateForm:: |
public | function | MappingCreator constructor. | |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
62 |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |