You are here

erl_layouts.module in Entity Reference with Layout 8

File

modules/erl_layouts/erl_layouts.module
View source
<?php

/**
 * @file
 * Contains entity_reference_layout.module.
 */
use Drupal\Core\Field\WidgetInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_field_widget_third_party_settings_form().
 *
 * Provide editable options for managing layout settings.
 */
function erl_layouts_field_widget_third_party_settings_form(WidgetInterface $plugin, FieldDefinitionInterface $field_definition, $form_mode, $form, FormStateInterface $form_state) {
  $element = [];

  // Add a 'my_setting' checkbox to the settings form for 'foo_widget' field
  // widgets.
  if ($plugin
    ->getPluginId() == 'entity_reference_layout_widget') {
    $field_name = $field_definition
      ->getName();
    $element['layout_class_mode'] = [
      '#type' => 'radios',
      '#title' => t('Method for adding class to layouts'),
      '#options' => _erl_layouts_get_mode_options(),
      '#default_value' => $plugin
        ->getThirdPartySetting('erl_layouts', 'layout_class_mode', key(_erl_layouts_get_mode_options())),
      '#description' => t('Select the method you want to use for adding class to layouts.'),
    ];
    $element['layout_class_force'] = [
      '#type' => 'textfield',
      '#title' => t('Layout classes'),
      '#default_value' => $plugin
        ->getThirdPartySetting('erl_layouts', 'layout_class_force'),
      '#description' => t('Set the classes to add on each layout. Classes must be separated by a space.'),
      '#attributes' => [
        'placeholder' => 'layout layout-default',
      ],
      '#size' => '30',
      '#states' => [
        'visible' => [
          ':input[name="fields[' . $field_name . '][settings_edit_form][third_party_settings][erl_layouts][layout_class_mode]"]' => [
            'value' => 'force',
          ],
        ],
      ],
    ];
    $element['layout_class_select'] = [
      '#type' => 'textarea',
      '#title' => t('Define possibles classes'),
      '#default_value' => $plugin
        ->getThirdPartySetting('erl_layouts', 'layout_class_select'),
      '#description' => _erl_layouts_select_class_description(),
      '#attributes' => [
        'placeholder' => 'layout layout--default|Default layout' . PHP_EOL . 'layout layout-reversed|Reversed layout',
      ],
      '#size' => '30',
      '#states' => [
        'visible' => [
          ':input[name="fields[' . $field_name . '][settings_edit_form][third_party_settings][erl_layouts][layout_class_mode]"]' => [
            'value' => 'select',
          ],
        ],
      ],
    ];
    $element['region_class_mode'] = [
      '#type' => 'radios',
      '#title' => t('Method for adding class to regions.'),
      '#options' => _erl_layouts_get_mode_options(),
      '#default_value' => $plugin
        ->getThirdPartySetting('erl_layouts', 'region_class_mode', key(_erl_layouts_get_mode_options())),
      '#description' => t('Select the method you want to use for adding class to regions.'),
    ];
    $element['region_class_force'] = [
      '#type' => 'textfield',
      '#title' => t('Region classes'),
      '#default_value' => $plugin
        ->getThirdPartySetting('erl_layouts', 'region_class_force'),
      '#description' => t('Set the classes to add on each region. Classes must be separated by a space.'),
      '#attributes' => [
        'placeholder' => 'region region-default',
      ],
      '#size' => '30',
      '#states' => [
        'visible' => [
          ':input[name="fields[' . $field_name . '][settings_edit_form][third_party_settings][erl_layouts][region_class_mode]"]' => [
            'value' => 'force',
          ],
        ],
      ],
    ];
    $element['region_class_select'] = [
      '#type' => 'textarea',
      '#title' => t('Define possibles classes'),
      '#default_value' => $plugin
        ->getThirdPartySetting('erl_layouts', 'region_class_select'),
      '#description' => _erl_layouts_select_class_description(),
      '#attributes' => [
        'placeholder' => 'region region--default|Default region' . PHP_EOL . 'region region-reversed|Reversed region',
      ],
      '#size' => '30',
      '#states' => [
        'visible' => [
          ':input[name="fields[' . $field_name . '][settings_edit_form][third_party_settings][erl_layouts][region_class_mode]"]' => [
            'value' => 'select',
          ],
        ],
      ],
    ];
    $element['layout_color_mode'] = [
      '#type' => 'radios',
      '#title' => t('Method for adding background color to layouts'),
      '#options' => _erl_layouts_get_mode_options(NULL, 'background color'),
      '#default_value' => $plugin
        ->getThirdPartySetting('erl_layouts', 'layout_color_mode', key(_erl_layouts_get_mode_options(NULL, 'background color'))),
      '#description' => t('Select the method you want to use for adding background color to layouts.'),
    ];
    $element['layout_color_force'] = [
      '#type' => 'textfield',
      '#title' => t('Layout background color'),
      '#default_value' => $plugin
        ->getThirdPartySetting('erl_layouts', 'layout_color_force'),
      '#description' => t('Set the background color to add on each layout.'),
      '#attributes' => [
        'placeholder' => '#fff|White',
      ],
      '#size' => '30',
      '#states' => [
        'visible' => [
          ':input[name="fields[' . $field_name . '][settings_edit_form][third_party_settings][erl_layouts][layout_color_mode]"]' => [
            'value' => 'force',
          ],
        ],
      ],
    ];
    $element['layout_color_select'] = [
      '#type' => 'textarea',
      '#title' => t('Define possibles background colors'),
      '#default_value' => $plugin
        ->getThirdPartySetting('erl_layouts', 'layout_color_select'),
      '#description' => _erl_layouts_select_class_description(),
      '#attributes' => [
        'placeholder' => '#fff|White' . PHP_EOL . '#000|Black',
      ],
      '#size' => '30',
      '#states' => [
        'visible' => [
          ':input[name="fields[' . $field_name . '][settings_edit_form][third_party_settings][erl_layouts][layout_color_mode]"]' => [
            'value' => 'select',
          ],
        ],
      ],
    ];
    $element['region_color_mode'] = [
      '#type' => 'radios',
      '#title' => t('Method for adding background color to regions'),
      '#options' => _erl_layouts_get_mode_options(NULL, 'background color'),
      '#default_value' => $plugin
        ->getThirdPartySetting('erl_layouts', 'region_color_mode', key(_erl_layouts_get_mode_options(NULL, 'background color'))),
      '#description' => t('Select the method you want to use for adding background color to regions.'),
    ];
    $element['region_color_force'] = [
      '#type' => 'textfield',
      '#title' => t('Region background color'),
      '#default_value' => $plugin
        ->getThirdPartySetting('erl_layouts', 'region_color_force'),
      '#description' => t('Set the background color to add on each region.'),
      '#attributes' => [
        'placeholder' => '#fff|White',
      ],
      '#size' => '30',
      '#states' => [
        'visible' => [
          ':input[name="fields[' . $field_name . '][settings_edit_form][third_party_settings][erl_layouts][region_color_mode]"]' => [
            'value' => 'force',
          ],
        ],
      ],
    ];
    $element['region_color_select'] = [
      '#type' => 'textarea',
      '#title' => t('Define possibles background colors'),
      '#default_value' => $plugin
        ->getThirdPartySetting('erl_layouts', 'region_color_select'),
      '#description' => _erl_layouts_select_class_description(),
      '#attributes' => [
        'placeholder' => '#fff|White' . PHP_EOL . '#000|Black',
      ],
      '#size' => '30',
      '#states' => [
        'visible' => [
          ':input[name="fields[' . $field_name . '][settings_edit_form][third_party_settings][erl_layouts][region_color_mode]"]' => [
            'value' => 'select',
          ],
        ],
      ],
    ];
  }
  return $element;
}

/**
 * Return the options availables for the widget.
 *
 * @param string|null $key
 *   The optionnal key to retrieve.
 * @param string|null $type
 *   The type to get mode options for (example: 'class' or 'color').
 *
 * @return array|mixed
 *   The options array or the value corresponding to $key.
 */
function _erl_layouts_get_mode_options($key = NULL, $type = 'class') {
  $options = [
    'manual' => t('Users can set a @type manually', [
      '@type' => $type,
    ]),
    'select' => t('Let users select a @type from a list', [
      '@type' => $type,
    ]),
    'force' => t('Automatically add the specified @type', [
      '@type' => $type,
    ]),
  ];
  if ($key && isset($options[$key])) {
    return $options[$key];
  }
  return $options;
}

/**
 * Return the description for the class select mode.
 */
function _erl_layouts_select_class_description() {
  $description = '<p>' . t('The possible classes a layout or region can have. Enter one value per line, in the format key|label.');
  $description .= '<br/>' . t('The key is the string which will be used as a class on the element. The label will be used on edit forms.');
  $description .= '<br/>' . t('If the key contains several classes, each class must be separated by a <strong>space</strong>.');
  $description .= '<br/>' . t('The label is optional: if a line contains a single string, it will be used as key and label.');
  $description .= '</p>';
  return $description;
}

Functions

Namesort descending Description
erl_layouts_field_widget_third_party_settings_form Implements hook_field_widget_third_party_settings_form().
_erl_layouts_get_mode_options Return the options availables for the widget.
_erl_layouts_select_class_description Return the description for the class select mode.