You are here

public function AddToCalForm::buildForm in Add to Cal 8

Parameters

array $form:

\Drupal\Core\Form\FormStateInterface $form_state:

null $entity:

null $settings:

Return value

array

Overrides FormInterface::buildForm

File

src/Form/AddToCalForm.php, line 57

Class

AddToCalForm
Class AddToCalForm

Namespace

Drupal\addtocal\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {

  // Need both entity and settings to do anything of note
  if (!is_object($this->entity) || empty($this->settings)) {
    return $form;
  }

  // Always open in new tab
  $form['#attributes']['target'] = '_blank';

  // CSS / JS Libraries
  $form['#attributes']['class'][] = 'addtocal-form';
  $form['#attached']['library'][] = 'addtocal/addtocal';

  // Pass through to submit
  $form['entity'] = [
    '#type' => 'value',
    '#value' => $this->entity,
  ];
  $form['settings'] = [
    '#type' => 'value',
    '#value' => $this->settings,
  ];
  $form['delta'] = [
    '#type' => 'value',
    '#value' => $this->delta,
  ];

  // Unique element based on field
  $entity_type = $this->entity
    ->bundle();
  $field_name = $this->settings['field_name'];
  $element_id = 'addtocal-' . $entity_type . '-' . $field_name . '-' . $this->entity
    ->id() . '--' . $this->delta;

  // Wrap the form output for easier styling
  $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'),
  ];

  // @TODO: make selection options part of the field config
  $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;
}