public function ProductForm::form in Commerce Core 8.2
Gets the actual form array to be built.
Overrides ContentEntityForm::form
See also
\Drupal\Core\Entity\EntityForm::processForm()
\Drupal\Core\Entity\EntityForm::afterBuild()
File
- modules/
product/ src/ Form/ ProductForm.php, line 79
Class
- ProductForm
- Defines the product add/edit form.
Namespace
Drupal\commerce_product\FormCode
public function form(array $form, FormStateInterface $form_state) {
/** @var \Drupal\commerce_product\Entity\ProductInterface $product */
$product = $this->entity;
$form = parent::form($form, $form_state);
$form['#tree'] = TRUE;
$form['#theme'] = [
'commerce_product_form',
];
$form['#attached']['library'][] = 'commerce_product/form';
// Changed must be sent to the client, for later overwrite error checking.
$form['changed'] = [
'#type' => 'hidden',
'#default_value' => $product
->getChangedTime(),
];
$form['status']['#group'] = 'footer';
$last_saved = $this
->t('Not saved yet');
if (!$product
->isNew()) {
$last_saved = $this->dateFormatter
->format($product
->getChangedTime(), 'short');
}
$form['meta'] = [
'#attributes' => [
'class' => [
'entity-meta__header',
],
],
'#type' => 'container',
'#group' => 'advanced',
'#weight' => -100,
'published' => [
'#type' => 'html_tag',
'#tag' => 'h3',
'#value' => $product
->isPublished() ? $this
->t('Published') : $this
->t('Not published'),
'#access' => !$product
->isNew(),
'#attributes' => [
'class' => [
'entity-meta__title',
],
],
],
'changed' => [
'#type' => 'item',
'#wrapper_attributes' => [
'class' => [
'entity-meta__last-saved',
'container-inline',
],
],
'#markup' => '<h4 class="label inline">' . $this
->t('Last saved') . '</h4> ' . $last_saved,
],
'author' => [
'#type' => 'item',
'#wrapper_attributes' => [
'class' => [
'author',
'container-inline',
],
],
'#markup' => '<h4 class="label inline">' . $this
->t('Author') . '</h4> ' . $product
->getOwner()
->getDisplayName(),
],
];
$form['advanced'] = [
'#type' => 'container',
'#attributes' => [
'class' => [
'entity-meta',
],
],
'#weight' => 99,
];
$form['visibility_settings'] = [
'#type' => 'details',
'#title' => $this
->t('Visibility settings'),
'#open' => TRUE,
'#group' => 'advanced',
'#access' => !empty($form['stores']['#access']),
'#attributes' => [
'class' => [
'product-visibility-settings',
],
],
'#weight' => 30,
];
$form['path_settings'] = [
'#type' => 'details',
'#title' => $this
->t('URL path settings'),
'#open' => !empty($form['path']['widget'][0]['alias']['#default_value']),
'#group' => 'advanced',
'#access' => !empty($form['path']['#access']) && $product
->get('path')
->access('edit'),
'#attributes' => [
'class' => [
'path-form',
],
],
'#attached' => [
'library' => [
'path/drupal.path',
],
],
'#weight' => 60,
];
$form['author'] = [
'#type' => 'details',
'#title' => $this
->t('Authoring information'),
'#group' => 'advanced',
'#attributes' => [
'class' => [
'product-form-author',
],
],
'#weight' => 90,
'#optional' => TRUE,
];
if (isset($form['uid'])) {
$form['uid']['#group'] = 'author';
}
if (isset($form['created'])) {
$form['created']['#group'] = 'author';
}
if (isset($form['path'])) {
$form['path']['#group'] = 'path_settings';
}
if (isset($form['stores'])) {
$form['stores']['#group'] = 'visibility_settings';
$form['#after_build'][] = [
get_class($this),
'hideEmptyVisibilitySettings',
];
}
return $form;
}