View source
<?php
namespace Drupal\addtocal\Form;
use Drupal\addtocal\AddToCalTypeManager;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
class AddToCalForm extends FormBase {
protected $entity;
protected $settings;
protected $delta;
public function __construct(EntityInterface $entity, $settings, $delta) {
$this->entity = $entity;
$this->settings = $settings;
$this->delta = $delta;
}
public function getFormId() {
$form_id = 'addtocal_' . $this->entity
->bundle() . '_' . $this->settings['field_name'] . '_' . $this->entity
->id();
return $form_id;
}
public function buildForm(array $form, FormStateInterface $form_state) {
if (!is_object($this->entity) || empty($this->settings)) {
return $form;
}
$form['#attributes']['target'] = '_blank';
$form['#attributes']['class'][] = 'addtocal-form';
$form['#attached']['library'][] = 'addtocal/addtocal';
$form['entity'] = [
'#type' => 'value',
'#value' => $this->entity,
];
$form['settings'] = [
'#type' => 'value',
'#value' => $this->settings,
];
$form['delta'] = [
'#type' => 'value',
'#value' => $this->delta,
];
$entity_type = $this->entity
->bundle();
$field_name = $this->settings['field_name'];
$element_id = 'addtocal-' . $entity_type . '-' . $field_name . '-' . $this->entity
->id() . '--' . $this->delta;
$form['addtocal_container'] = [
'#type' => 'container',
'#attributes' => [
'class' => [
'addtocal-container',
],
],
];
$form['addtocal_container']['addtocal_btn'] = [
'#type' => 'container',
'#attributes' => [
'class' => [
'addtocal',
],
],
'#id' => $element_id,
'#markup' => $this
->t('Add to Calendar'),
];
$form['addtocal_container']['type'] = [
'#type' => 'radios',
'#options' => [
'generic' => $this
->t('iCal / Outlook'),
'google' => $this
->t('Google'),
'yahoo' => $this
->t('Yahoo!'),
],
'#attributes' => [
'class' => [
'addtocal-menu',
],
'onclick' => 'this.form.submit();',
],
'#id' => $element_id . '-menu',
];
$form['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Submit'),
'#attributes' => [
'class' => [
'addtocal-submit',
],
],
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$values = $form_state
->getValues();
$entity = $values['entity'];
$settings = $values['settings'];
$delta = $values['delta'];
$type = $values['type'];
$manager = \Drupal::service('plugin.manager.addtocal.type');
$instance = $manager
->createInstance($type);
$instance
->downloadSubmit($entity, $settings, $delta, $form_state);
}
}