You are here

function lazy_field_formatter_third_party_settings_form in Lazy-load 8.3

Same name and namespace in other branches
  1. 8 lazy.module \lazy_field_formatter_third_party_settings_form()
  2. 8.2 lazy.module \lazy_field_formatter_third_party_settings_form()

Implements hook_field_formatter_third_party_settings_form().

File

./lazy.module, line 75
Module file for Lazy-load.

Code

function lazy_field_formatter_third_party_settings_form(FormatterInterface $plugin, FieldDefinitionInterface $field_definition, $view_mode, array $form, FormStateInterface $form_state) {
  $element = [];
  if (in_array($plugin
    ->getPluginId(), lazy_field_formatters(), TRUE)) {
    $element['lazy_image'] = [
      '#type' => 'checkbox',
      '#title' => t('Enable lazy-loading'),
      '#default_value' => $plugin
        ->getThirdPartySetting('lazy', 'lazy_image', FALSE),
    ];
    if (in_array($plugin
      ->getPluginId(), [
      'lazy_image',
      'lazy_responsive_image',
    ])) {
      $element['lazy_image']['#default_value'] = TRUE;
      $element['lazy_image']['#disabled'] = TRUE;
    }
    $element['placeholder_style'] = [
      '#title' => t('Placeholder image style'),
      '#description' => t('When an image style is selected it would be used to generate the placeholder image. Otherwise the <a href=":url">shared settings</a> apply.', [
        ':url' => Url::fromRoute('lazy.config_form')
          ->toString(),
      ]),
      '#type' => 'select',
      '#default_value' => $plugin
        ->getThirdPartySetting('lazy', 'placeholder_style', ''),
      '#options' => image_style_options(),
      '#states' => [
        'visible' => [
          ':input[name*="[settings_edit_form][third_party_settings][lazy][lazy_image]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $element['data_uri'] = [
      '#type' => 'checkbox',
      '#title' => t('Use data URIs for the placeholder image'),
      '#description' => t('When checked, the content of the linked image is embedded into the HTML. i.e. <code>data:image/gif;base64,R0lGODdhAQABAPAAAP8A...</code><br>See <a href=":url">Can I use "Data URIs"?</a>', [
        ':url' => 'https://caniuse.com/#feat=datauri',
      ]),
      '#default_value' => $plugin
        ->getThirdPartySetting('lazy', 'data_uri', 0),
      '#states' => [
        'visible' => [
          [
            ':input[name*="[settings_edit_form][third_party_settings][lazy][lazy_image]"]' => [
              'checked' => TRUE,
            ],
          ],
        ],
        'invisible' => [
          [
            ':input[name*="[settings_edit_form][third_party_settings][lazy][placeholder_style]"]' => [
              'value' => '',
            ],
          ],
        ],
      ],
    ];
  }
  return $element;
}