class GroupContentTypeForm in Group 2.0.x
Same name and namespace in other branches
- 8 src/Entity/Form/GroupContentTypeForm.php \Drupal\group\Entity\Form\GroupContentTypeForm
Form controller for group content type forms.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, RedirectDestinationTrait, StringTranslationTrait
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
- class \Drupal\group\Entity\Form\GroupContentTypeForm
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
Expanded class hierarchy of GroupContentTypeForm
File
- src/
Entity/ Form/ GroupContentTypeForm.php, line 14
Namespace
Drupal\group\Entity\FormView source
class GroupContentTypeForm extends EntityForm {
/**
* The group relation plugin manager.
*
* @var \Drupal\group\Plugin\Group\Relation\GroupRelationManagerInterface
*/
protected $pluginManager;
/**
* Constructs a new GroupContentTypeForm.
*
* @param \Drupal\group\Plugin\Group\Relation\GroupRelationManagerInterface $plugin_manager
* The group content plugin manager.
*/
public function __construct(GroupRelationManagerInterface $plugin_manager) {
$this->pluginManager = $plugin_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('plugin.manager.group_relation'));
}
/**
* Returns the configurable plugin for the group content type.
*
* @return \Drupal\group\Plugin\Group\Relation\GroupRelationInterface
* The configurable group relation plugin.
*/
protected function getRelationPlugin() {
/** @var \Drupal\group\Entity\GroupContentTypeInterface $group_content_type */
$group_content_type = $this
->getEntity();
$group_type = $group_content_type
->getGroupType();
// Initialize an empty plugin so we can show a default configuration form.
if ($this->operation == 'add') {
$plugin_id = $group_content_type
->getRelationPluginId();
$configuration['group_type_id'] = $group_type
->id();
return $this->pluginManager
->createInstance($plugin_id, $configuration);
}
else {
return $group_content_type
->getRelationPlugin();
}
}
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
/** @var \Drupal\group\Entity\GroupContentTypeInterface $group_content_type */
$group_content_type = $this
->getEntity();
$group_type = $group_content_type
->getGroupType();
$plugin = $this
->getRelationPlugin();
// @todo These messages may need some love.
if ($this->operation == 'add') {
$form['#title'] = $this
->t('Install content plugin');
$message = 'By installing the %plugin plugin, you will allow %entity_type entities to be added to groups of type %group_type';
}
else {
$form['#title'] = $this
->t('Configure content plugin');
$message = 'This form allows you to configure the %plugin plugin for the %group_type group type.';
}
// Add in the replacements for the $message variable set above.
$replace = [
'%plugin' => $plugin
->getLabel(),
'%entity_type' => $this->entityTypeManager
->getDefinition($plugin
->getEntityTypeId())
->getLabel(),
'%group_type' => $group_type
->label(),
];
// Display a description to explain the purpose of the form.
$form['description'] = [
'#markup' => $this
->t($message, $replace),
];
// Add in the plugin configuration form.
$form += $plugin
->buildConfigurationForm($form, $form_state);
return $form;
}
/**
* {@inheritdoc}
*/
protected function actions(array $form, FormStateInterface $form_state) {
$actions['submit'] = [
'#type' => 'submit',
'#value' => $this->operation == 'add' ? $this
->t('Install plugin') : $this
->t('Save configuration'),
'#submit' => [
'::submitForm',
],
];
return $actions;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$plugin = $this
->getRelationPlugin();
$plugin
->validateConfigurationForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
/** @var \Drupal\group\Entity\GroupContentTypeInterface $group_content_type */
$group_content_type = $this
->getEntity();
$group_type = $group_content_type
->getGroupType();
$plugin = $this
->getRelationPlugin();
$plugin
->submitConfigurationForm($form, $form_state);
// Remove button and internal Form API values from submitted values.
$form_state
->cleanValues();
// Extract the values as configuration that should be saved.
$config = $form_state
->getValues();
// If we are on an 'add' form, we create the group content type using the
// plugin configuration submitted using this form.
if ($this->operation == 'add') {
/** @var \Drupal\group\Entity\Storage\GroupContentTypeStorageInterface $storage */
$storage = $this->entityTypeManager
->getStorage('group_content_type');
$storage
->createFromPlugin($group_type, $plugin
->getPluginId(), $config)
->save();
$this
->messenger()
->addStatus($this
->t('The content plugin was installed on the group type.'));
}
else {
$group_content_type
->updateContentPlugin($config);
$this
->messenger()
->addStatus($this
->t('The content plugin configuration was saved.'));
}
$form_state
->setRedirect('entity.group_type.content_plugins', [
'group_type' => $group_type
->id(),
]);
}
/**
* {@inheritdoc}
*/
public function getEntityFromRouteMatch(RouteMatchInterface $route_match, $entity_type_id) {
if ($route_match
->getRawParameter($entity_type_id) !== NULL) {
return $route_match
->getParameter($entity_type_id);
}
// If we are on the create form, we can't extract an entity from the route,
// so we need to create one based on the route parameters.
$values = [];
if ($route_match
->getRawParameter('group_type') !== NULL && $route_match
->getRawParameter('plugin_id') !== NULL) {
$values = [
'group_type' => $route_match
->getRawParameter('group_type'),
'content_plugin' => $route_match
->getRawParameter('plugin_id'),
];
}
return $this->entityTypeManager
->getStorage($entity_type_id)
->create($values);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
EntityForm:: |
protected | property | The entity being used by this form. | 11 |
EntityForm:: |
protected | property | The entity type manager. | 3 |
EntityForm:: |
protected | property | The module handler service. | |
EntityForm:: |
protected | property | The name of the current operation. | |
EntityForm:: |
protected | function | Returns the action form element for the current entity form. | |
EntityForm:: |
public | function | Form element #after_build callback: Updates the entity with submitted data. | |
EntityForm:: |
public | function |
Builds an updated entity object based upon the submitted form values. Overrides EntityFormInterface:: |
3 |
EntityForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
13 |
EntityForm:: |
protected | function | Copies top-level form values to entity properties. | 9 |
EntityForm:: |
public | function |
Returns a string identifying the base form. Overrides BaseFormIdInterface:: |
6 |
EntityForm:: |
public | function |
Gets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
12 |
EntityForm:: |
public | function |
Gets the operation identifying the form. Overrides EntityFormInterface:: |
|
EntityForm:: |
protected | function | Initialize the form state and the entity before the first form build. | 3 |
EntityForm:: |
protected | function | Prepares the entity object before the form is built first. | 3 |
EntityForm:: |
protected | function | Invokes the specified prepare hook variant. | |
EntityForm:: |
public | function | Process callback: assigns weights and hides extra fields. | |
EntityForm:: |
public | function |
Form submission handler for the 'save' action. Overrides EntityFormInterface:: |
47 |
EntityForm:: |
public | function |
Sets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity type manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the module handler for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the operation for this form. Overrides EntityFormInterface:: |
|
FormBase:: |
protected | property | The config factory. | 3 |
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. | 3 |
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. | |
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. | |
GroupContentTypeForm:: |
protected | property | The group relation plugin manager. | |
GroupContentTypeForm:: |
protected | function |
Returns an array of supported actions for the current entity form. Overrides EntityForm:: |
|
GroupContentTypeForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
GroupContentTypeForm:: |
public | function |
Gets the actual form array to be built. Overrides EntityForm:: |
|
GroupContentTypeForm:: |
public | function |
Determines which entity will be used by this form from a RouteMatch object. Overrides EntityForm:: |
|
GroupContentTypeForm:: |
protected | function | Returns the configurable plugin for the group content type. | |
GroupContentTypeForm:: |
public | function |
This is the default entity object builder function. It is called before any
other submit handler to build the new entity object to be used by the
following submit handlers. At this point of the form workflow the entity is
validated and the form state… Overrides EntityForm:: |
|
GroupContentTypeForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
GroupContentTypeForm:: |
public | function | Constructs a new GroupContentTypeForm. | |
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. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
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. | 4 |
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. |