class ShareMessageForm in Share Message 8
Base form controller for Share Message edit forms.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
- class \Drupal\sharemessage\Form\ShareMessageForm
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
Expanded class hierarchy of ShareMessageForm
File
- src/
Form/ ShareMessageForm.php, line 18
Namespace
Drupal\sharemessage\FormView source
class ShareMessageForm extends EntityForm {
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* Share plugin manager.
*
* @var \Drupal\sharemessage\SharePluginManager
*/
protected $sharePluginManager;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The entity type bundle info.
*
* @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
*/
protected $entityTypeBundleInfo;
/**
* Constructs a new ShareMessageForm object.
*
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
* @param \Drupal\sharemessage\SharePluginManager $share_manager
* The share manager.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
* The entity type bundle info.
*/
public function __construct(ModuleHandlerInterface $module_handler, SharePluginManager $share_manager, EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info) {
$this->moduleHandler = $module_handler;
$this->sharePluginManager = $share_manager;
$this->entityTypeManager = $entity_type_manager;
$this->entityTypeBundleInfo = $entity_type_bundle_info;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('module_handler'), $container
->get('plugin.manager.sharemessage.share'), $container
->get('entity_type.manager'), $container
->get('entity_type.bundle.info'));
}
/**
* Overrides Drupal\Core\Entity\EntityFormController::form().
*/
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
/** @var \Drupal\sharemessage\ShareMessageInterface $sharemessage */
$sharemessage = $this->entity;
$defaults = \Drupal::config('sharemessage.settings');
$available = $this->sharePluginManager
->getLabels();
$form['label'] = [
'#type' => 'textfield',
'#title' => t('Label'),
'#required' => TRUE,
'#default_value' => $sharemessage
->label(),
'#weight' => -3,
];
$form['id'] = [
'#type' => 'machine_name',
'#title' => t('Machine Name'),
'#machine_name' => [
'exists' => 'sharemessage_check_machine_name_if_exist',
'source' => [
'label',
],
],
'#required' => TRUE,
'#weight' => -2,
'#disabled' => !$sharemessage
->isNew(),
'#default_value' => $sharemessage
->id(),
];
$form['title'] = [
'#type' => 'textfield',
'#title' => t('Title'),
'#default_value' => $sharemessage->title,
'#description' => t('Used as title in the Share Message, where applicable: Facebook, E-Mail subject, ...'),
'#weight' => 5,
];
$form['message_long'] = [
'#type' => 'textarea',
'#title' => t('Long Description'),
'#default_value' => $sharemessage->message_long,
'#description' => t('Used as long description for the Share Message, where applicable: Facebook, Email body, ...'),
'#weight' => 10,
];
$form['message_short'] = [
'#type' => 'textfield',
'#title' => t('Short Description'),
'#default_value' => $sharemessage->message_short,
'#description' => t('Used as short description for twitter messages.'),
'#weight' => 15,
];
$form['video_url'] = [
'#type' => 'textfield',
'#title' => t('Video URL'),
'#default_value' => $sharemessage->video_url,
'#description' => t('The video URL that will be used for sharing.'),
'#weight' => 18,
];
$form['image_url'] = [
'#type' => 'textfield',
'#title' => t('Image URL'),
'#default_value' => $sharemessage->image_url,
'#description' => t('The image URL that will be used for sharing. If a video URL is set, the image is used as a thumbnail for the video.'),
'#weight' => 20,
];
$form['image_width'] = [
'#type' => 'textfield',
'#title' => t('Image Width'),
'#default_value' => $sharemessage->image_width,
'#description' => t('The width of the image that will be used for sharing.'),
'#weight' => 21,
];
$form['image_height'] = [
'#type' => 'textfield',
'#title' => t('Image Height'),
'#default_value' => $sharemessage->image_height,
'#description' => t('The height of the image that will be used for sharing.'),
'#weight' => 22,
];
// @todo: Convert this to a file upload/selection widget.
$form['fallback_image'] = [
'#type' => 'textfield',
'#title' => t('Fallback image (File UUID)'),
'#default_value' => $sharemessage->fallback_image,
'#description' => t('Specify a static fallback image that is used if the Image URL is empty (For example, when tokens are used and the specified image field is empty).'),
'#weight' => 23,
];
$form['share_url'] = [
'#type' => 'textfield',
'#title' => t('Shared URL'),
'#default_value' => $sharemessage->share_url,
'#description' => t('Specific URL that will be shared, defaults to the current page.'),
'#weight' => 25,
];
// If the Share Message plugin is not set, pick AddThis plugin as the
// default.
if (!$sharemessage
->hasPlugin()) {
$sharemessage
->setPluginID('addthis');
}
$form['plugin_wrapper'] = [
'#type' => 'container',
'#prefix' => '<div id="sharemessage-plugin-wrapper">',
'#suffix' => '</div>',
];
$definition = $this->sharePluginManager
->getDefinition($sharemessage
->getPluginID());
if ($sharemessage
->hasPlugin()) {
$form['plugin_wrapper']['plugin'] = [
'#type' => 'select',
'#title' => t('Share Message plugin'),
'#description' => isset($definition['description']) ? Xss::filter($definition['description']) : '',
'#options' => $available,
'#default_value' => $sharemessage
->getPluginID(),
'#required' => TRUE,
'#ajax' => [
'callback' => [
$this,
'ajaxShareMessagePluginSelect',
],
'wrapper' => 'sharemessage-plugin-wrapper',
],
];
$form['plugin_wrapper']['plugin_select'] = [
'#type' => 'submit',
'#value' => $this
->t('Select plugin'),
'#submit' => [
'::ajaxShareMessagePluginSelect',
],
'#attributes' => [
'class' => [
'js-hide',
],
],
];
$form['plugin_wrapper']['settings'] = [
'#type' => 'details',
'#title' => t('@plugin plugin settings', [
'@plugin' => $definition['label'],
]),
'#tree' => TRUE,
'#open' => TRUE,
];
// Add the Share Message plugin settings form.
$form['plugin_wrapper']['settings'] += $sharemessage
->getPlugin()
->buildConfigurationForm($form['plugin_wrapper']['settings'], $form_state);
if (!Element::children($form['plugin_wrapper']['settings'])) {
// Settings fieldset.
$form['plugin_wrapper']['settings']['override_default_settings'] = [
'#type' => 'item',
'#description' => t("The @plugin plugin doesn't provide any settings.", [
'@plugin' => $sharemessage
->getPluginDefinition()['label'],
]),
];
}
}
if ($defaults
->get('message_enforcement')) {
$form['enforce_usage'] = [
'#type' => 'checkbox',
'#title' => t('Enforce the usage of this Share Message on the page it points to'),
'#description' => t('If checked, this Share Message will be used on the page that it is referring to and override the Share Message there.'),
'#default_value' => $sharemessage->enforce_usage ?: 0,
'#weight' => 40,
];
}
// Define a form to expose a Share Message as an extra field for a given
// entity type and its bundle(s).
$form['sharemessage_extra_field'] = [
'#type' => 'container',
'#prefix' => '<div id="sharemessage-extra-field">',
'#suffix' => '</div>',
'#weight' => 50,
];
// If the entity type ajax has been triggered, store the new entity type
// used to display its bundles on the UI.
$current_type = $sharemessage
->getExtraFieldEntityType();
if ($form_state
->hasValue('entity_type') && $form_state
->getValue('entity_type') !== $current_type) {
$sharemessage
->setExtraFieldEntityType($form_state
->getValue('entity_type'));
$current_type = $sharemessage
->getExtraFieldEntityType();
}
// Get the entity types that have view builder.
$extra_field_entity_type_options = [];
foreach ($this->entityTypeManager
->getDefinitions() as $entity_type_id => $definition) {
if ($definition
->getGroup() == 'content' && $definition
->hasHandlerClass('view_builder')) {
// Check whether entity type has any view displays.
$view_display_ids = \Drupal::entityQuery('entity_view_display')
->condition('targetEntityType', $entity_type_id)
->count()
->execute();
if ($view_display_ids) {
$extra_field_entity_type_options[$entity_type_id] = $definition
->getLabel();
}
}
}
// Get the bundles of the selected content entity type.
$extra_field_bundle_options = [];
if ($current_type) {
$extra_field_bundle_options = $this
->getBundles($current_type);
}
// Define the entity type select form.
$form['sharemessage_extra_field']['entity_type'] = [
'#type' => 'select',
'#title' => t('Share Message extra field'),
'#description' => t('Select an entity type, then check the content entities where you want to show this Share Message. This only displays entity types which have at least one view display configured. In case an entity type does not show up, go to "Manage display" and press "Save".'),
'#options' => $extra_field_entity_type_options,
'#empty_option' => t('- None -'),
'#empty_value' => '',
'#default_value' => $current_type,
'#required' => FALSE,
'#ajax' => [
'callback' => [
$this,
'ajaxShareMessageContentTypeSelect',
],
'wrapper' => 'sharemessage-extra-field',
],
];
$form['sharemessage_extra_field']['entity_type_select'] = [
'#type' => 'submit',
'#value' => $this
->t('Select entity type'),
'#submit' => [
'::ajaxShareMessageContentTypeSelect',
],
'#attributes' => [
'class' => [
'js-hide',
],
],
];
// Show the selected entity type's bundles, if there are any. Check the
// ones that have been selected. Otherwise don't check any to allow all.
if ($current_type) {
$entity_type = $this->entityTypeManager
->getDefinition($current_type);
if ($entity_type
->hasKey('bundle')) {
// If all bundles have been allowed, don't select any.
$enabled_all = array_diff(array_keys($extra_field_bundle_options), $sharemessage
->getExtraFieldBundles());
// Get the selected bundles.
$extra_field_bundles = array_intersect($sharemessage
->getExtraFieldBundles(), array_keys($extra_field_bundle_options));
$form['sharemessage_extra_field']['bundles'] = [
'#type' => 'checkboxes',
'#title' => $this
->getEntityBundleLabel($entity_type),
'#description' => t("Per default this extra field is not visible. Go to the bundle's Manage display page to enable it. Select none to allow all @content_entity.", [
'@content_entity' => strtolower($this
->getEntityBundleLabel($entity_type)),
]),
'#options' => $extra_field_bundle_options,
'#default_value' => empty($enabled_all) ? [] : $extra_field_bundles,
'#tree' => TRUE,
'#open' => TRUE,
];
}
}
// Update sharemessage_token_help according to the selected entity type.
if ($this->moduleHandler
->moduleExists('token')) {
// If '- None -' option is selected, show 'node' as default.
$token_type = $current_type ? \Drupal::service('token.entity_mapper')
->getTokenTypeForEntityType($current_type) : 'node';
$form['sharemessage_extra_field']['sharemessage_token_help'] = [
'#title' => t('Replacement patterns'),
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#description' => t('These tokens can be used in all text fields.'),
'#weight' => 2,
];
$form['sharemessage_extra_field']['sharemessage_token_help']['browser'] = [
'#theme' => 'token_tree_link',
'#token_types' => [
$token_type,
'sharemessage',
],
];
}
return $form;
}
/**
* Ajax callback to fetch the selected Share Message settings.
*
* @param array $form
* A nested array form elements comprising the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
public function ajaxShareMessagePluginSelect(array $form, FormStateInterface $form_state) {
return $form['plugin_wrapper'];
}
/**
* Ajax callback to fetch the selected content type with view builder.
*
* @param array $form
* A nested array form elements comprising the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
public function ajaxShareMessageContentTypeSelect(array $form, FormStateInterface $form_state) {
return $form['sharemessage_extra_field'];
}
/**
* {@inheritdoc}
*/
public function buildEntity(array $form, FormStateInterface $form_state) {
/** @var \Drupal\sharemessage\ShareMessageInterface $sharemessage */
$sharemessage = parent::buildEntity($form, $form_state);
if (!$sharemessage
->getSetting('override_default_settings')) {
$sharemessage->settings = [];
}
// Store the selected content entities where the Share Message will be
// displayed as an extra field into the config entity. Otherwise unset
// extra_field_bundles if no specific entity type has been selected.
$sharemessage
->setExtraFieldEntityType($form_state
->getValue('entity_type'));
$sharemessage
->setExtraFieldBundles($form_state
->hasValue('bundles') ? array_keys(array_filter($form_state
->getValue('bundles'))) : []);
// Move the override field into the settings array.
// if (\Drupal::config('sharemessage.settings')->get('message_enforcement')) {
// $sharemessage->settings['enforce_usage'] = $sharemessage->enforce_usage;
// unset($sharemessage->enforce_usage);
// }
return $sharemessage;
}
/**
* Provides the bundle label with a fallback when not defined.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type we are looking the bundle label for.
*
* @return \Drupal\Core\StringTranslation\TranslatableMarkup
* The entity bundle label or a fallback label.
*/
protected function getEntityBundleLabel($entity_type) {
if ($label = $entity_type
->getBundleLabel()) {
return $this
->t('@label', [
'@label' => $label,
]);
}
$fallback = $entity_type
->getLabel();
if ($bundle_entity_type = $entity_type
->getBundleEntityType()) {
// This is a better fallback.
$fallback = $this->entityTypeManager
->getDefinition($bundle_entity_type)
->getLabel();
}
return $this
->t('@label bundle', [
'@label' => $fallback,
]);
}
/**
* Gets the bundles of the selected content entity type.
*
* @param string $entity_type
* The entity type where to retrieve the bundles.
*
* @return array
* An associative array of bundle's labels keyed by the bundle's ID.
*/
protected function getBundles($entity_type) {
$extra_field_bundle_options = [];
foreach ($this->entityTypeBundleInfo
->getBundleInfo($entity_type) as $bundle => $info) {
$extra_field_bundle_options[$bundle] = $info['label'];
}
return $extra_field_bundle_options;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
/** @var \Drupal\sharemessage\ShareMessageInterface $sharemessage */
$sharemessage = $this->entity;
$sharemessage
->getPlugin()
->validateConfigurationForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
/** @var \Drupal\sharemessage\ShareMessageInterface $sharemessage */
$sharemessage = $this->entity;
$status = $sharemessage
->save();
if ($status == SAVED_UPDATED) {
$this
->messenger()
->addMessage(t('Share Message %label has been updated.', [
'%label' => $sharemessage
->label(),
]));
}
else {
$this
->messenger()
->addMessage(t('Share Message %label has been added.', [
'%label' => $sharemessage
->label(),
]));
}
// Share Message settings might have changed, but it is not immediately
// updated for the view display. Thus clear the entity extra field caches.
\Drupal::service('entity_field.manager')
->clearCachedFieldDefinitions();
$form_state
->setRedirect('entity.sharemessage.collection');
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
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 | |
EntityForm:: |
protected | property | The entity being used by this form. | 7 |
EntityForm:: |
protected | property | The name of the current operation. | |
EntityForm:: |
private | property | The entity manager. | |
EntityForm:: |
protected | function | Returns an array of supported actions for the current entity form. | 29 |
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 |
Form constructor. Overrides FormInterface:: |
10 |
EntityForm:: |
protected | function | Copies top-level form values to entity properties | 7 |
EntityForm:: |
public | function |
Returns a string identifying the base form. Overrides BaseFormIdInterface:: |
5 |
EntityForm:: |
public | function |
Gets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Determines which entity will be used by this form from a RouteMatch object. Overrides EntityFormInterface:: |
1 |
EntityForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
10 |
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 |
Sets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity manager for this form. 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:: |
|
EntityForm:: |
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 FormInterface:: |
17 |
EntityForm:: |
public | function | ||
EntityForm:: |
public | function | ||
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. | |
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. | |
ShareMessageForm:: |
protected | property | The entity type bundle info. | |
ShareMessageForm:: |
protected | property |
The entity type manager. Overrides EntityForm:: |
|
ShareMessageForm:: |
protected | property |
The module handler. Overrides EntityForm:: |
|
ShareMessageForm:: |
protected | property | Share plugin manager. | |
ShareMessageForm:: |
public | function | Ajax callback to fetch the selected content type with view builder. | |
ShareMessageForm:: |
public | function | Ajax callback to fetch the selected Share Message settings. | |
ShareMessageForm:: |
public | function |
Builds an updated entity object based upon the submitted form values. Overrides EntityForm:: |
|
ShareMessageForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
ShareMessageForm:: |
public | function |
Overrides Drupal\Core\Entity\EntityFormController::form(). Overrides EntityForm:: |
|
ShareMessageForm:: |
protected | function | Gets the bundles of the selected content entity type. | |
ShareMessageForm:: |
protected | function | Provides the bundle label with a fallback when not defined. | |
ShareMessageForm:: |
public | function |
Form submission handler for the 'save' action. Overrides EntityForm:: |
|
ShareMessageForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
ShareMessageForm:: |
public | function | Constructs a new ShareMessageForm object. | |
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. |