You are here

public function AceFormatter::viewElements in Ace Code Editor 8

Builds a renderable array for a field value.

Parameters

\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.

string $langcode: The language that should be used to render the field.

Return value

array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.

Overrides FormatterInterface::viewElements

File

src/Plugin/Field/FieldFormatter/AceFormatter.php, line 191

Class

AceFormatter
Plugin implementation of the 'ace_editor' formatter.

Namespace

Drupal\ace_editor\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {

  // Renders front-end of our formatter.
  $elements = [];
  $settings = $this
    ->getSettings();
  foreach ($items as $delta => $item) {
    $elements[$delta] = [
      '#type' => 'textarea',
      '#value' => $item->value,
      // Attach libraries as per the setting.
      '#attached' => [
        'library' => [
          'ace_editor/formatter',
        ],
        'drupalSettings' => [
          // Pass settings variable ace_formatter to javascript.
          'ace_formatter' => $settings,
        ],
      ],
      '#attributes' => [
        'class' => [
          'content',
        ],
        'readonly' => 'readonly',
      ],
      '#prefix' => '<div class="ace_formatter">',
      '#suffix' => '<div>',
    ];
  }
  return $elements;
}