Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
drupal/dx8_keystone / src / KeystoneFormElementBase.php
Size: Mime:
<?php

namespace Drupal\dx8_keystone;

use Drupal\cohesion_elements\CustomElementPluginBase;
use Drupal\Core\Template\Attribute;

/**
 * Base class for Keystone forms. This contains default fields
 * based on `string` or `boolean` config options passed to the
 * `FormRenderer.createForm(config);` method.
 *
 * @package Drupal\keystone
 */
class KeystoneFormElementBase extends CustomElementPluginBase {

  /**
   * {@inheritdoc}
   */
  public function getFields() {
    return [
      'formUri' => [
        // This is the bootstrap class name that will be applied to the wrapping column.
        'htmlClass' => 'col-xs-12',
        // All form elements require a title.
        'title' => 'Form URI (required)',
        // The field type.
        'type' => 'textfield',
        // These fields are specific to this form field type.
        'placeholder' => 'Should be unique from other forms',
      ],
      'formId' => [
        // This is the bootstrap class name that will be applied to the wrapping column.
        'htmlClass' => 'col-xs-12',
        // All form elements require a title.
        'title' => 'Form ID (required)',
        // The field type.
        'type' => 'textfield',
        // These fields are specific to this form field type.
        'placeholder' => 'Defaults to the element\'s class name',
      ],
      'noHtml5Validate' => [
        'htmlClass' => 'col-xs-3',
        'type' => 'checkbox',
        'title' => 'noHtml5Validate',
        // These fields are specific to this form field type.
        'notitle' => false,
        'defaultValue' => false,
      ],
      'showErrorList' => [
        'htmlClass' => 'col-xs-3',
        'type' => 'checkbox',
        'title' => 'showErrorList',
        // These fields are specific to this form field type.
        'notitle' => false,
        'defaultValue' => false,
      ],
      'successMessage' => [
        'htmlClass' => 'col-xs-12',
        'type' => 'wysiwyg',
        'title' => 'Success Message',
        'defaultValue' => [
          'text' => '<p>This is some default content.</p>',
          'textFormat' => 'cohesion'
        ],
      ],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function render($element_settings, $element_markup, $element_class, $element_context = []) {

    // Create form id to pass to twig template and front end settings.
    $formId = empty($element_settings['formId']) ? $element_class : $element_settings['formId'];

    if (empty($element_settings['formUri'])) {
      \Drupal::messenger()->addError('Please add value for Form URI field to ' . $this->getLabel());
      return;
    }

    if (empty($formId)) {
      \Drupal::messenger()->addError('Please add value for Form ID field to ' . $this->getLabel());
      return;
    }

    // Create array that we'll add HTML attributes to.
    $attributes = [];

    // Add default class.
    $attributes['class'][] = 'keystone-form';

    // Add element class that DX8 creates that starts with `coh-ce-xxxxxxx`.
    if (!empty($element_class)) {
      $attributes['class'][] = $element_class;
    }

    // Add `classes` from Markup tab.
    if (!empty($element_markup['classes'])) {
      $attributes['class'][] = $element_markup['classes'];
    }

    // Add `id` attribute from Markup tab.
    if (!empty($element_markup['id'])) {
      $attributes['id'] = $element_markup['id'];
    }

    // Add `attributes` from the Markup tab.
    if (!empty($element_markup['attributes'])) {
      foreach ($element_markup['attributes'] as $attr) {
        $attributes[$attr['attribute']] = $attr['value'];
      }
    }

    $attributes['data-form-id'] = $formId;

    // Create initial render array.
    $renderArr = [
      '#theme' => 'keystone_form',
      '#elementSettings' => $element_settings,
      '#elementMarkup' => $element_markup,
      '#elementContext' => $element_context,
      '#elementClass' => $element_class,
      '#elementAttributes' => new Attribute($attributes),
      '#formId' => $formId,
      '#cache' => [
        'tags' => ['keystone', $formId, $element_class],
      ]
    ];

    // Attach drupalSettings to the front end.
    $renderArr['#attached']['drupalSettings']['keystone'][$formId] = [
      'formId' => $formId,
      'formUri' => $element_settings['formUri'] ?: '',
      'noHtml5Validate' => $element_settings['noHtml5Validate'] ?: FALSE,
      'showErrorList' => $element_settings['showErrorList'] ?: FALSE,
      'successMessage' => $element_settings['successMessage'] ?: '',
    ];

    // Attach the library to the render array.
    $renderArr['#attached']['library'][] = 'keystone/form-renderer';

    // Render the element.
    return $renderArr;
  }

}