You are here

commerce_pado.module in Commerce Product Add-on 8

Same filename and directory in other branches
  1. 7 commerce_pado.module

Commerce Product Add On module file.

File

commerce_pado.module
View source
<?php

/**
 * @file
 * Commerce Product Add On module file.
 */
use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_theme().
 */
function commerce_pado_theme($existing, $type, $theme, $path) {
  return [
    'commerce_pado_add_to_cart_form' => [
      'render element' => 'form',
    ],
    'commerce_pado_addon_product_label' => [
      'variables' => [
        'product_entity' => NULL,
      ],
    ],
    'commerce_pado_addon_product_variation_label' => [
      'variables' => [
        'product_entity' => NULL,
        'variation_entity' => NULL,
      ],
    ],
  ];
}

/**
 * Implements hook_theme_suggestions_commerce_pado_addon_product_label().
 */
function commerce_pado_theme_suggestions_commerce_pado_addon_product_label(array $variables) {
  $original = $variables['theme_hook_original'];
  $suggestions = [];
  $suggestions[] = $original;
  $suggestions[] = $original . '__' . $variables['product_entity']
    ->bundle();
  return $suggestions;
}

/**
 * Implements hook_theme_suggestions_commerce_pado_addon_product_variation_label().
 */
function commerce_pado_theme_suggestions_commerce_pado_addon_product_variation_label(array $variables) {
  $original = $variables['theme_hook_original'];
  $suggestions = [];
  $suggestions[] = $original;
  $suggestions[] = $original . '__' . $variables['variation_entity']
    ->bundle();
  return $suggestions;
}

/**
 * Implements hook_entity_type_build().
 */
function commerce_pado_entity_type_build(array &$entity_types) {
  $entity_types['commerce_order_item']
    ->setFormClass('pado_add_to_cart', '\\Drupal\\commerce_pado\\Form\\PadoAddToCartForm');
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function commerce_pado_form_entity_view_display_edit_form_alter(&$form, FormStateInterface $form_state) {
  if ($form['#entity_type'] === 'commerce_product') {
    $form['#validate'][] = 'commerce_pado_entity_view_display_form_validate';
  }
}

/**
 * Form validate callback form the view display edit form.
 *
 * Checking if a product add-on field has been selected for the
 * commerce_pado_add_to_cart field formatter.
 */
function commerce_pado_entity_view_display_form_validate(&$form, FormStateInterface $form_state) {
  if ($form_state
    ->getTriggeringElement()['#array_parents'] == [
    'actions',
    'submit',
  ]) {

    //** @var \Drupal\Core\Entity\Entity\EntityViewDisplay $entity
    $entity = $form_state
      ->getFormObject()
      ->getEntity();
    $content = $entity
      ->get('content');
    if (!empty($content['variations']) && $content['variations']['type'] === 'commerce_pado_add_to_cart') {
      if (empty($content['variations']['settings']['add_on_field'])) {
        $form_state
          ->setError($form['fields']['variations']['plugin']['type'], t('Please select a product add-on field.'));
      }
    }
  }
}

Functions

Namesort descending Description
commerce_pado_entity_type_build Implements hook_entity_type_build().
commerce_pado_entity_view_display_form_validate Form validate callback form the view display edit form.
commerce_pado_form_entity_view_display_edit_form_alter Implements hook_form_FORM_ID_alter().
commerce_pado_theme Implements hook_theme().
commerce_pado_theme_suggestions_commerce_pado_addon_product_label Implements hook_theme_suggestions_commerce_pado_addon_product_label().
commerce_pado_theme_suggestions_commerce_pado_addon_product_variation_label Implements hook_theme_suggestions_commerce_pado_addon_product_variation_label().