You are here

public static function Branding::addFieldFormatterBranding in Formatter Suite 8

Adds branding to a field formatter form.

This function wraps a field formatter's form with <div>s and adds a logo image to the top. Module styling then adds a background, sets colors, etc.

@internal Field formatter plugins are used by the Field UI and Views UI modules to present an administrator form to control the formatting of a field in an entity. The Field UI and Views UI modules use AJAX to insert the plugin's form within a larger form.

Structurally the inserted form is expected to have one form element for each setting defined by the plugin. The form elements must be named after the setting and they cannot be nested within a container, or any other render or form structure.

This required structure prevents us from introducing a wrapper render element around the form, then styling that wrapper. Instead, this function adds a '#prefix' and '#suffix' to the form, plus an attached branding library. The prefix adds a <div> and a logo image, while the suffix closes the <div>. @endinternal

Parameters

array $form: The renderable form array to which to add branding items.

Return value

array Returns the form with the branding added.

11 calls to Branding::addFieldFormatterBranding()
EntityListTrait::settingsForm in src/Plugin/Field/FieldFormatter/EntityListTrait.php
GeneralEmailFormatter::settingsForm in src/Plugin/Field/FieldFormatter/GeneralEmailFormatter.php
Returns a form to configure settings for the formatter.
GeneralEntityReferenceFormatter::settingsForm in src/Plugin/Field/FieldFormatter/GeneralEntityReferenceFormatter.php
Returns a form to configure settings for the formatter.
GeneralFileLinkFormatter::settingsForm in src/Plugin/Field/FieldFormatter/GeneralFileLinkFormatter.php
Returns a form to configure settings for the formatter.
GeneralImageFormatter::settingsForm in src/Plugin/Field/FieldFormatter/GeneralImageFormatter.php
Returns a form to configure settings for the formatter.

... See full list

File

src/Branding.php, line 126

Class

Branding
Defines functions and constants used for branding.

Namespace

Drupal\formatter_suite

Code

public static function addFieldFormatterBranding(array &$form) {

  //
  // Setup
  // -----
  // Get module information.
  $module = \Drupal::moduleHandler()
    ->getModule('formatter_suite');
  $moduleName = $module
    ->getName();
  $modulePath = '/' . $module
    ->getPath() . '/';
  $moduleImagesPath = $modulePath . self::MODULE_IMAGES_SUBDIRECTORY . '/';

  // Get the path to the module's logo.
  $logoPath = $moduleImagesPath . self::LOGO_FILE_NAME;

  // Define some CSS classes.
  $wrapperClass = 'formatter_suite-field-formatter-settings';
  $logoClass = 'formatter_suite-branding-logo';

  //
  // Create prefix & suffix HTML
  // ---------------------------
  // Create the image HTML.
  $logoImage = '<img class="' . $logoClass . '" alt="' . $moduleName . '" src="' . $logoPath . '">';

  // Create the prefix and suffix.
  $form['#prefix'] = '<div class="' . $wrapperClass . '">' . $logoImage;
  $form['#suffix'] = '</div>';

  // Attach the module's branding library.
  $form['#attached']['library'][] = self::MODULE_BRANDING_LIBRARY;
  return $form;
}