Skip to content

Instantly share code, notes, and snippets.

@lonalore
Created June 7, 2016 09:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lonalore/2a304f89a8cee7e977cbb6100f486949 to your computer and use it in GitHub Desktop.
Save lonalore/2a304f89a8cee7e977cbb6100f486949 to your computer and use it in GitHub Desktop.
<?php
namespace Drupal\paypal_roles\Form;
use Drupal\Core\Ajax\ReplaceCommand;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Ajax\AjaxResponse;
/**
* Form controller for Membership edit forms.
*
* @ingroup paypal_roles
*/
class MembershipEntityForm extends ContentEntityForm {
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
/* @var $entity \Drupal\paypal_roles\Entity\MembershipEntity */
$form = parent::buildForm($form, $form_state);
$values = $form_state->getValues();
$entity = $this->entity;
if (isset($form['role']['widget']['#options']['anonymous'])) {
unset($form['role']['widget']['#options']['anonymous']);
}
if (isset($form['role']['widget']['#options']['authenticated'])) {
unset($form['role']['widget']['#options']['authenticated']);
}
$form['cancellation']['#states'] = array(
'visible' => array(
':input[name="type"]' => array('value' => '1'),
),
);
$form['cycles']['#states'] = array(
'invisible' => array(
':input[name="type"]' => array('value' => '0'),
),
);
// Container for inline display.
$form['length_container'] = array(
'#type' => 'container',
'#attributes' => array(
'class' => array('container-inline', 'length-container'),
),
'#weight' => $form['length']['#weight'],
);
// Hide title by 'length_type' element.
$form['length_type']['widget']['#title_display'] = 'invisible';
// Copy 'length' element into container.
$form['length_container']['length'] = $form['length'];
// Copy 'length_type' element into container.
$form['length_container']['length_type'] = $form['length_type'];
// Set weight for 'length_type' element for displaying it after 'length'.
$form['length_container']['length_type']['#weight'] = $form['length']['#weight'] + 1;
// Append a small description.
$form['length_container']['length_desc'] = array(
'#type' => 'item',
'#description' => t('The length a Member has access to the Membership can be set to a specified number of Days, Weeks, Months or Years.') . '<br>' . t('If you want to create a simple one-time purchase button with no-expiration time, set this value to 0.'),
'#weight' => $form['length']['#weight'] + 2,
);
// Unset copied (moved) form elements.
unset($form['length']);
unset($form['length_type']);
// Set an Ajax callback on currency.
$form['currency']['widget']['#ajax'] = array(
// Function to call when event on form element triggered.
'callback' => array($this, 'currencyChangedAjax'),
// Effect when replacing content. Options: 'none' (default), 'slide', 'fade'.
'effect' => 'fade',
// Javascript event to trigger Ajax. Currently for: 'onchange'.
'event' => 'change',
'progress' => array(
// Graphic shown to indicate ajax. Options: 'throbber' (default), 'bar'.
'type' => 'throbber',
// Message to show along progress graphic. Default: 'Please wait...'.
'message' => NULL,
),
);
// Container for inline display.
$form['amount_container'] = array(
'#type' => 'container',
'#attributes' => array(
'class' => array('container-inline', 'amount-container'),
),
'#weight' => $form['amount']['#weight'],
);
// Hide title by 'currency' element.
$form['currency']['widget']['#title_display'] = 'invisible';
// Copy 'amount' element into container.
$form['amount_container']['amount'] = $form['amount'];
// Copy 'currency' element into container.
$form['amount_container']['currency'] = $form['currency'];
// Set weight for 'currency' element for displaying it after 'amount'.
$form['amount_container']['currency']['#weight'] = $form['amount']['#weight'] + 1;
// Append a small description.
$form['amount_container']['amount_desc'] = array(
'#type' => 'item',
'#description' => t('The amount of subscription / one-time purchase.') . '<br />' . t('HUF, JPY, TWD: These currencies do not support decimals. Passing a decimal amount will throw an error.'),
'#weight' => $form['amount']['#weight'] + 2,
);
// Unset copied (moved) form elements.
unset($form['amount']);
unset($form['currency']);
// Container for inline display.
$form['discount_container'] = array(
'#type' => 'container',
'#attributes' => array(
'class' => array('container-inline', 'discount-container'),
),
'#weight' => $form['discount_rate']['#weight'],
'#states' => array(
'visible' => array(
':input[name="type"]' => array('value' => '0'),
),
),
);
// Hide title by 'discount_role' element.
$form['discount_role']['widget']['#title_display'] = 'invisible';
// Rename 'None' option.
// $form['discount_role']['widget']['#options']['_none'] = t('- everyone -');
// Prepend helper text.
$form['discount_role']['widget']['#prefix'] = '% ' . t('only for this role:');
// Copy 'discount_rate' element into container.
$form['discount_container']['discount_rate'] = $form['discount_rate'];
// Copy 'discount_role' element into container.
$form['discount_container']['discount_role'] = $form['discount_role'];
// Set weight for 'discount_role' element for displaying it after 'discount_rate'.
$form['discount_container']['discount_role']['#weight'] = $form['discount_rate']['#weight'] + 1;
// Append a small description.
$form['discount_container']['discount_desc'] = array(
'#type' => 'item',
'#description' => t('PayPal allows discount rate only for one-time purchase.'),
'#weight' => $form['discount_rate']['#weight'] + 2,
);
// Unset copied (moved) form elements.
unset($form['discount_rate']);
unset($form['discount_role']);
$curr = isset($values['currency'][0]['value']) ? $values['currency'][0]['value'] : FALSE;
$currency = $curr ? $curr : 'EUR';
// Container for inline display.
$form['trial_length_container'] = array(
'#type' => 'container',
'#attributes' => array(
'class' => array('container-inline', 'trial-length-container'),
),
'#weight' => $form['trial_amount']['#weight'],
'#states' => array(
'invisible' => array(
':input[name="type"]' => array('value' => '0'),
),
),
);
// Hide title by 'trial_length' element.
$form['trial_length']['widget'][0]['value']['#title_display'] = 'invisible';
// Prepend helper text.
$form['trial_length']['widget']['#prefix'] = ' ' . $currency . ' ' . t('for');
// Hide title by 'trial_length_type' element.
$form['trial_length_type']['widget']['#title_display'] = 'invisible';
// Copy 'trial_amount' element into container.
$form['trial_length_container']['trial_amount'] = $form['trial_amount'];
// Copy 'trial_length' element into container.
$form['trial_length_container']['trial_length'] = $form['trial_length'];
// Set weight for 'trial_length' element for displaying it after 'trial_amount'.
$form['trial_length_container']['trial_length']['#weight'] = $form['trial_amount']['#weight'] + 1;
// Copy 'trial_length_type' element into container.
$form['trial_length_container']['trial_length_type'] = $form['trial_length_type'];
// Set weight for 'trial_length_type' element for displaying it after 'trial_length'.
$form['trial_length_container']['trial_length_type']['#weight'] = $form['trial_length']['#weight'] + 1;
// Unset copied (moved) form elements.
unset($form['trial_amount']);
unset($form['trial_length']);
unset($form['trial_length_type']);
// Attach custom CSS to fix inline layout.
$form['#attached'] = array(
'library' => array(
'paypal_roles/paypal_roles.forms',
),
);
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$values = $form_state->getValues();
// If Recurring payment.
if (!empty($values['type'][0]['value']) && (int) $values['type'][0]['value'] === 1) {
if (!empty($values['length'][0]['value']) && (int) $values['length'][0]['value'] === 0) {
$form_state->setErrorByName('length', t('Length of subscription cannot be 0. Value 0 is allowed only for one-time purchase.'));
}
if (!empty($values['cycles'][0]['value']) && (int) $values['cycles'][0]['value'] === 1) {
$form_state->setErrorByName('cycles', t('Billing cycles cannot be 1. Set billing cycles to 0, or set a value, which is higher than 1.'));
}
if (!empty($values['trial_amount'][0]['value'])) {
if ((strlen(strrchr($values['trial_amount'][0]['value'], '.')) - 1) > 2) {
$form_state->setErrorByName('trial_amount', t('Please set the Amount of Trial Period value as a number rounded to 2 decimal places.'));
}
if ((float) $values['trial_amount'][0]['value'] > 0) {
if ((int) $values['trial_length'][0]['value'] <= 0) {
$form_state->setErrorByName('trial_length', t('Trial Period length cannot be 0.'));
}
}
}
}
if (!empty($values['amount'][0]['value'])) {
if ((strlen(strrchr($values['amount'][0]['value'], '.')) - 1) > 2) {
$form_state->setErrorByName('amount', t('Please set the Amount value as a number rounded to 2 decimal places.'));
}
}
parent::validateForm($form, $form_state);
}
/**
* Ajax callback to change currencies on form.
*/
public function currencyChangedAjax(array &$form, FormStateInterface $form_state) {
$values = $form_state->getValues();
$curr = isset($values['currency'][0]['value']) ? $values['currency'][0]['value'] : FALSE;
$currency = $curr ? $curr : 'EUR';
$form['trial_length_container']['trial_length']['widget']['#prefix'] = ' ' . $currency . ' ' . t('for');
// Instantiate an AjaxResponse Object to return.
$ajax_response = new AjaxResponse();
$ajax_response->addCommand(new ReplaceCommand('.field--name-trial-length', $form['trial_length_container']['trial_length']));
return $ajax_response;
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$entity = $this->entity;
$status = parent::save($form, $form_state);
switch ($status) {
case SAVED_NEW:
drupal_set_message($this->t('Created the %label Membership.', [
'%label' => $entity->label(),
]));
break;
default:
drupal_set_message($this->t('Saved the %label Membership.', [
'%label' => $entity->label(),
]));
}
$form_state->setRedirect('entity.membership.collection');
}
}
@lonalore
Copy link
Author

lonalore commented Jun 7, 2016

kepernyokep - 2016-06-07 - 12 09 00

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment