You are here

SmsSendWidget.php in SMS Framework 2.x

File

modules/sms_sendtophone/src/Plugin/Field/FieldWidget/SmsSendWidget.php
View source
<?php

declare (strict_types=1);
namespace Drupal\sms_sendtophone\Plugin\Field\FieldWidget;

use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Plugin implementation of the 'sms_sendtophone' widget.
 *
 * @FieldWidget(
 *   id = "sms_sendtophone",
 *   label = @Translation("Text Field and SMS send to phone"),
 *   field_types = {
 *     "text"
 *   }
 * )
 */
class SmsSendWidget extends WidgetBase {

  /**
   * {@inheritdoc}
   */
  public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
    $field = $this->fieldDefinition;
    $element['value'] = $element + [
      '#title' => $this
        ->getSetting('label'),
      '#default_value' => $items[$delta]->value,
      '#required' => $field
        ->isRequired(),
      '#description' => $this
        ->getSetting('description'),
      '#maxlength' => $field
        ->getSetting('max_length'),
      '#weight' => $this
        ->getSetting('weight'),
    ];
    if ($this
      ->getSetting('rows') == 1) {
      $element['#type'] = 'textfield';
    }
    else {
      $element['#type'] = 'textarea';
      $element['#rows'] = $this
        ->getSetting('rows');
    }
    return $element;
  }

  /**
   * {@inheritdoc}
   */
  public static function defaultSettings() {
    return [
      'rows' => 1,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, FormStateInterface $form_state) {
    $form = parent::settingsForm($form, $form_state);
    $form['rows'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Rows'),
      '#default_value' => $this->settings['rows'],
      '#required' => TRUE,
    ];
    return $form;
  }

}

Classes

Namesort descending Description
SmsSendWidget Plugin implementation of the 'sms_sendtophone' widget.