You are here

ButtonFieldHtml.php in Button Field 8

Contains \Drupal\button_field\Plugin\field\formatter\ButtonFieldHtml.

File

src/Plugin/Field/FieldFormatter/ButtonFieldHtml.php
View source
<?php

/**
 * @file
 * Contains \Drupal\button_field\Plugin\field\formatter\ButtonFieldHtml.
 */
namespace Drupal\button_field\Plugin\Field\FieldFormatter;

use Drupal\Core\Form\FormStateInterface;

/**
 * Plugin implementation of the 'html button' field formatter.
 *
 * @FieldFormatter(
 *   id = "button_field_html",
 *   label = @Translation("HTML Button"),
 *   description = @Translation("An HTML button formatter."),
 *   field_types = {
 *     "button_field"
 *   }
 * )
 */
class ButtonFieldHtml extends ButtonFieldBase {

  /**
   * {@inheritdoc}
   */
  public static function defaultSettings() {
    return array(
      'text' => '',
    ) + parent::defaultSettings();
  }

  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, FormStateInterface $form_state) {
    $element = parent::settingsForm($form, $form_state);
    $element['text'] = array(
      '#type' => 'textfield',
      '#title' => t('Button Text'),
      '#default_value' => $this
        ->getSetting('text') ?: $this->fieldDefinition
        ->getLabel(),
      '#required' => TRUE,
    );
    return $element;
  }

  /**
   * {@inheritdoc}
   */
  public function settingsSummary() {
    $summary = array();
    $summary[] = t('Button title: !text', array(
      '!text' => $this
        ->getSetting('text') ?: $this->fieldDefinition
        ->getLabel(),
    ));
    return $summary;
  }

  /**
   * {@inheritdoc}
   */
  protected function elementProperties() {
    return array(
      '#type' => 'button',
      '#value' => $this
        ->getSetting('text') ?: $this->fieldDefinition
        ->getLabel(),
    );
  }

}

Classes

Namesort descending Description
ButtonFieldHtml Plugin implementation of the 'html button' field formatter.