class ProductAttributeForm in Commerce Core 8.2
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\Core\Entity\BundleEntityFormBase
- class \Drupal\commerce_product\Form\ProductAttributeForm
- class \Drupal\Core\Entity\BundleEntityFormBase
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
Expanded class hierarchy of ProductAttributeForm
File
- modules/
product/ src/ Form/ ProductAttributeForm.php, line 14
Namespace
Drupal\commerce_product\FormView source
class ProductAttributeForm extends BundleEntityFormBase {
/**
* The attribute field manager.
*
* @var \Drupal\commerce_product\ProductAttributeFieldManagerInterface
*/
protected $attributeFieldManager;
/**
* The inline form manager.
*
* @var \Drupal\commerce\InlineFormManager
*/
protected $inlineFormManager;
/**
* Constructs a new ProductAttributeForm object.
*
* @param \Drupal\commerce_product\ProductAttributeFieldManagerInterface $attribute_field_manager
* The attribute field manager.
* @param \Drupal\commerce\InlineFormManager $inline_form_manager
* The inline form manager.
*/
public function __construct(ProductAttributeFieldManagerInterface $attribute_field_manager, InlineFormManager $inline_form_manager) {
$this->attributeFieldManager = $attribute_field_manager;
$this->inlineFormManager = $inline_form_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('commerce_product.attribute_field_manager'), $container
->get('plugin.manager.commerce_inline_form'));
}
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
/** @var \Drupal\commerce_product\Entity\ProductAttributeInterface $attribute */
$attribute = $this->entity;
$form['label'] = [
'#type' => 'textfield',
'#title' => $this
->t('Name'),
'#maxlength' => 255,
'#default_value' => $attribute
->label(),
'#required' => TRUE,
];
$form['id'] = [
'#type' => 'machine_name',
'#default_value' => $attribute
->id(),
'#machine_name' => [
'exists' => '\\Drupal\\commerce_product\\Entity\\ProductAttribute::load',
],
// Attribute field names are constructed as 'attribute_' + id, and must
// not be longer than 32 characters. Account for that prefix length here.
'#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH - 10,
'#disabled' => !$attribute
->isNew(),
];
$form['elementType'] = [
'#type' => 'select',
'#title' => $this
->t('Element type'),
'#description' => $this
->t('Controls how the attribute is displayed on the add to cart form.'),
'#options' => [
'radios' => $this
->t('Radio buttons'),
'select' => $this
->t('Select list'),
'commerce_product_rendered_attribute' => $this
->t('Rendered attribute'),
],
'#default_value' => $attribute
->getElementType(),
];
$attribute_field_map = $this->attributeFieldManager
->getFieldMap();
$variation_type_storage = $this->entityTypeManager
->getStorage('commerce_product_variation_type');
$variation_types = $variation_type_storage
->loadMultiple();
// Allow the attribute to be assigned to a product variation type.
$form['original_variation_types'] = [
'#type' => 'value',
'#value' => [],
];
$form['variation_types'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Product variation types'),
'#options' => EntityHelper::extractLabels($variation_types),
'#access' => count($variation_types) > 0,
];
$disabled_variation_types = [];
foreach ($variation_types as $variation_type_id => $variation_type) {
if (!$attribute
->isNew() && isset($attribute_field_map[$variation_type_id])) {
$used_attributes = array_column($attribute_field_map[$variation_type_id], 'attribute_id');
if (in_array($attribute
->id(), $used_attributes)) {
$form['original_variation_types']['#value'][$variation_type_id] = $variation_type_id;
$form['variation_types']['#default_value'][$variation_type_id] = $variation_type_id;
if (!$this->attributeFieldManager
->canDeleteField($attribute, $variation_type_id)) {
$form['variation_types'][$variation_type_id] = [
'#disabled' => TRUE,
];
$disabled_variation_types[] = $variation_type_id;
}
}
}
}
$form['disabled_variation_types'] = [
'#type' => 'value',
'#value' => $disabled_variation_types,
];
if ($this->moduleHandler
->moduleExists('content_translation')) {
$enabled = TRUE;
if (!$attribute
->isNew()) {
$translation_manager = \Drupal::service('content_translation.manager');
$enabled = $translation_manager
->isEnabled('commerce_product_attribute_value', $attribute
->id());
}
$form['enable_value_translation'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Enable attribute value translation'),
'#default_value' => $enabled,
];
}
// The attribute acts as a bundle for attribute values, so the values can't
// be created until the attribute is saved.
if (!$attribute
->isNew()) {
$form = $this
->buildValuesForm($form, $form_state);
}
return $form;
}
/**
* Builds the attribute values form.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @return array
* The attribute values form.
*/
public function buildValuesForm(array $form, FormStateInterface $form_state) {
/** @var \Drupal\commerce_product\Entity\ProductAttributeInterface $attribute */
$attribute = $this->entity;
$values = $attribute
->getValues();
$user_input = $form_state
->getUserInput();
// Reorder the values by name, if requested.
if ($form_state
->get('reset_alphabetical')) {
$value_names = EntityHelper::extractLabels($values);
asort($value_names);
foreach (array_keys($value_names) as $weight => $id) {
$values[$id]
->setWeight($weight);
}
}
// The value map allows new values to be added and removed before saving.
// An array in the $index => $id format. $id is '_new' for unsaved values.
$value_map = (array) $form_state
->get('value_map');
if (empty($value_map)) {
$value_map = $values ? array_keys($values) : [
'_new',
];
$form_state
->set('value_map', $value_map);
}
$wrapper_id = Html::getUniqueId('product-attribute-values-ajax-wrapper');
$form['values'] = [
'#type' => 'table',
'#header' => [
[
'data' => $this
->t('Value'),
'colspan' => 2,
],
$this
->t('Weight'),
$this
->t('Operations'),
],
'#tabledrag' => [
[
'action' => 'order',
'relationship' => 'sibling',
'group' => 'product-attribute-value-order-weight',
],
],
'#weight' => 5,
'#prefix' => '<div id="' . $wrapper_id . '">',
'#suffix' => '</div>',
// #input defaults to TRUE, which breaks file fields on the value form.
// This table is used for visual grouping only, the element itself
// doesn't have any values of its own that need processing.
'#input' => FALSE,
];
// Make the weight list always reflect the current number of values.
// Taken from WidgetBase::formMultipleElements().
$max_weight = count($value_map);
foreach ($value_map as $index => $id) {
$value_form =& $form['values'][$index];
// The tabledrag element is always added to the first cell in the row,
// so we add an empty cell to guide it there, for better styling.
$value_form['#attributes']['class'][] = 'draggable';
$value_form['tabledrag'] = [
'#markup' => '',
];
if ($id == '_new') {
$value = $this->entityTypeManager
->getStorage('commerce_product_attribute_value')
->create([
'attribute' => $attribute
->id(),
'langcode' => $attribute
->get('langcode'),
]);
$default_weight = $max_weight;
$remove_access = TRUE;
}
else {
$value = $values[$id];
$default_weight = $value
->getWeight();
$remove_access = $value
->access('delete');
}
$inline_form = $this->inlineFormManager
->createInstance('content_entity', [
'skip_save' => TRUE,
], $value);
$value_form['entity'] = [
'#parents' => [
'values',
$index,
'entity',
],
'#inline_form' => $inline_form,
];
$value_form['entity'] = $inline_form
->buildInlineForm($value_form['entity'], $form_state);
$value_form['weight'] = [
'#type' => 'weight',
'#title' => $this
->t('Weight'),
'#title_display' => 'invisible',
'#delta' => $max_weight,
'#default_value' => $default_weight,
'#attributes' => [
'class' => [
'product-attribute-value-order-weight',
],
],
];
// Used by SortArray::sortByWeightProperty to sort the rows.
if (isset($user_input['values'][$index])) {
$input_weight = $user_input['values'][$index]['weight'];
// If the weights were just reset, reflect it in the user input.
if ($form_state
->get('reset_alphabetical')) {
$input_weight = $default_weight;
}
// Make sure the weight is not out of bounds due to removals.
if ($user_input['values'][$index]['weight'] > $max_weight) {
$input_weight = $max_weight;
}
// Reflect the updated user input on the element.
$value_form['weight']['#value'] = $input_weight;
$value_form['#weight'] = $input_weight;
}
else {
$value_form['#weight'] = $default_weight;
}
$value_form['remove'] = [
'#type' => 'submit',
'#name' => 'remove_value' . $index,
'#value' => $this
->t('Remove'),
'#limit_validation_errors' => [],
'#submit' => [
'::removeValueSubmit',
],
'#value_index' => $index,
'#ajax' => [
'callback' => '::valuesAjax',
'wrapper' => $wrapper_id,
],
'#access' => $remove_access,
];
}
// Sort the values by weight. Ensures weight is preserved on ajax refresh.
uasort($form['values'], [
'\\Drupal\\Component\\Utility\\SortArray',
'sortByWeightProperty',
]);
$access_handler = $this->entityTypeManager
->getAccessControlHandler('commerce_product_attribute_value');
if ($access_handler
->createAccess($attribute
->id())) {
$form['values']['_add_new'] = [
'#tree' => FALSE,
];
$form['values']['_add_new']['entity'] = [
'#type' => 'container',
'#wrapper_attributes' => [
'colspan' => 2,
],
];
$form['values']['_add_new']['entity']['add_value'] = [
'#type' => 'submit',
'#value' => $this
->t('Add value'),
'#submit' => [
'::addValueSubmit',
],
'#limit_validation_errors' => [],
'#ajax' => [
'callback' => '::valuesAjax',
'wrapper' => $wrapper_id,
],
];
$form['values']['_add_new']['entity']['reset_alphabetical'] = [
'#type' => 'submit',
'#value' => $this
->t('Reset to alphabetical'),
'#submit' => [
'::resetAlphabeticalSubmit',
],
'#limit_validation_errors' => [],
'#ajax' => [
'callback' => '::valuesAjax',
'wrapper' => $wrapper_id,
],
];
$form['values']['_add_new']['operations'] = [
'data' => [],
];
}
return $form;
}
/**
* Ajax callback for value operations.
*/
public function valuesAjax(array $form, FormStateInterface $form_state) {
return $form['values'];
}
/**
* Submit callback for adding a new value.
*/
public function addValueSubmit(array $form, FormStateInterface $form_state) {
$value_map = (array) $form_state
->get('value_map');
$value_map[] = '_new';
$form_state
->set('value_map', $value_map);
$form_state
->setRebuild();
}
/**
* Submit callback for resetting attribute value ordering to alphabetical.
*/
public function resetAlphabeticalSubmit(array $form, FormStateInterface $form_state) {
$form_state
->set('reset_alphabetical', TRUE);
$form_state
->setRebuild();
}
/**
* Submit callback for removing a value.
*/
public function removeValueSubmit(array $form, FormStateInterface $form_state) {
$value_index = $form_state
->getTriggeringElement()['#value_index'];
$value_map = (array) $form_state
->get('value_map');
$value_id = $value_map[$value_index];
unset($value_map[$value_index]);
$form_state
->set('value_map', $value_map);
// Non-new values also need to be deleted from storage.
if ($value_id != '_new') {
$delete_queue = (array) $form_state
->get('delete_queue');
$delete_queue[] = $value_id;
$form_state
->set('delete_queue', $delete_queue);
}
$form_state
->setRebuild();
}
/**
* Saves the attribute values.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
protected function saveValues(array $form, FormStateInterface $form_state) {
$delete_queue = $form_state
->get('delete_queue');
if (!empty($delete_queue)) {
$value_storage = $this->entityTypeManager
->getStorage('commerce_product_attribute_value');
$values = $value_storage
->loadMultiple($delete_queue);
$value_storage
->delete($values);
}
foreach ($form_state
->getValue([
'values',
]) as $index => $value_data) {
/** @var \Drupal\commerce\Plugin\Commerce\InlineForm\EntityInlineFormInterface $inline_form */
$inline_form = $form['values'][$index]['entity']['#inline_form'];
/** @var \Drupal\commerce_product\Entity\ProductAttributeValueInterface $value */
$value = $inline_form
->getEntity();
$value
->setWeight($value_data['weight']);
$value
->save();
}
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$status = $this->entity
->save();
$original_variation_types = $form_state
->getValue('original_variation_types', []);
$variation_types = array_filter($form_state
->getValue('variation_types', []));
$disabled_variation_types = $form_state
->getValue('disabled_variation_types', []);
$variation_types = array_unique(array_merge($disabled_variation_types, $variation_types));
$selected_variation_types = array_diff($variation_types, $original_variation_types);
$unselected_variation_types = array_diff($original_variation_types, $variation_types);
if ($selected_variation_types) {
foreach ($selected_variation_types as $selected_variation_type) {
$this->attributeFieldManager
->createField($this->entity, $selected_variation_type);
}
}
if ($unselected_variation_types) {
foreach ($unselected_variation_types as $unselected_variation_type) {
$this->attributeFieldManager
->deleteField($this->entity, $unselected_variation_type);
}
}
if ($this->moduleHandler
->moduleExists('content_translation')) {
$translation_manager = \Drupal::service('content_translation.manager');
// Logic from content_translation_language_configuration_element_submit().
$enabled = $form_state
->getValue('enable_value_translation');
if ($translation_manager
->isEnabled('commerce_product_attribute_value', $this->entity
->id()) != $enabled) {
$translation_manager
->setEnabled('commerce_product_attribute_value', $this->entity
->id(), $enabled);
$this->entityTypeManager
->clearCachedDefinitions();
\Drupal::service('router.builder')
->setRebuildNeeded();
}
}
if ($status == SAVED_NEW) {
$this
->messenger()
->addMessage($this
->t('Created the %label product attribute.', [
'%label' => $this->entity
->label(),
]));
// Send the user to the edit form to create the attribute values.
$form_state
->setRedirectUrl($this->entity
->toUrl('edit-form'));
}
else {
$this
->saveValues($form, $form_state);
$this
->messenger()
->addMessage($this
->t('Updated the %label product attribute.', [
'%label' => $this->entity
->label(),
]));
$form_state
->setRedirectUrl($this->entity
->toUrl('collection'));
}
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
BundleEntityFormBase:: |
protected | function | Protects the bundle entity's ID property's form element against changes. | |
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 entity type manager. | 3 |
EntityForm:: |
protected | property | The module handler service. | |
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 |
Builds an updated entity object based upon the submitted form values. Overrides EntityFormInterface:: |
2 |
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. | |
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. | |
ProductAttributeForm:: |
protected | property | The attribute field manager. | |
ProductAttributeForm:: |
protected | property | The inline form manager. | |
ProductAttributeForm:: |
public | function | Submit callback for adding a new value. | |
ProductAttributeForm:: |
public | function | Builds the attribute values form. | |
ProductAttributeForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
ProductAttributeForm:: |
public | function |
Gets the actual form array to be built. Overrides EntityForm:: |
|
ProductAttributeForm:: |
public | function | Submit callback for removing a value. | |
ProductAttributeForm:: |
public | function | Submit callback for resetting attribute value ordering to alphabetical. | |
ProductAttributeForm:: |
public | function |
Form submission handler for the 'save' action. Overrides EntityForm:: |
|
ProductAttributeForm:: |
protected | function | Saves the attribute values. | |
ProductAttributeForm:: |
public | function | Ajax callback for value operations. | |
ProductAttributeForm:: |
public | function | Constructs a new ProductAttributeForm object. | |
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. |