Same name and namespace in other branches
  1. 8.x-2.x src/Plugin/Field/FieldFormatter/ColorFieldFormatterCss.php \Drupal\color_field\Plugin\Field\FieldFormatter\ColorFieldFormatterCss::settingsForm() 1 comment

File

src/Plugin/Field/FieldFormatter/ColorFieldFormatterCss.php, line 94

Class

ColorFieldFormatterCss
Plugin implementation of the color_field css declaration formatter.

Namespace

Drupal\color_field\Plugin\Field\FieldFormatter

Code

public function settingsForm(array $form, FormStateInterface $form_state) : array {
    $elements = [];
    $elements['selector'] = [
        '#title' => $this->t('Selector'),
        '#description' => $this->t('A valid CSS selector such as <code>.links > li > a, #logo</code>. You can use tokens as shown below.'),
        '#type' => 'textarea',
        '#rows' => '1',
        '#default_value' => $this->getSetting('selector'),
        '#required' => TRUE,
        '#placeholder' => 'body > div > a',
        '#states' => [
            'visible' => [
                ':input[name="fields[' . $this->fieldDefinition
                    ->getName() . '][settings_edit_form][settings][advanced]"]' => [
                    'checked' => FALSE,
                ],
            ],
        ],
    ];
    $elements['property'] = [
        '#title' => $this->t('Property'),
        '#type' => 'select',
        '#default_value' => $this->getSetting('property'),
        '#required' => TRUE,
        '#options' => [
            'background-color' => $this->t('Background color'),
            'color' => $this->t('Text color'),
        ],
        '#states' => [
            'visible' => [
                ':input[name="fields[' . $this->fieldDefinition
                    ->getName() . '][settings_edit_form][settings][advanced]"]' => [
                    'checked' => FALSE,
                ],
            ],
        ],
    ];
    $elements['important'] = [
        '#title' => $this->t('Important'),
        '#description' => $this->t('Whenever this declaration is more important than others.'),
        '#type' => 'checkbox',
        '#default_value' => $this->getSetting('important'),
        '#states' => [
            'visible' => [
                ':input[name="fields[' . $this->fieldDefinition
                    ->getName() . '][settings_edit_form][settings][advanced]"]' => [
                    'checked' => FALSE,
                ],
            ],
        ],
    ];
    if ($this->getFieldSetting('opacity')) {
        $elements['opacity'] = [
            '#type' => 'checkbox',
            '#title' => $this->t('Display opacity'),
            '#default_value' => $this->getSetting('opacity'),
            '#states' => [
                'visible' => [
                    ':input[name="fields[' . $this->fieldDefinition
                        ->getName() . '][settings_edit_form][settings][advanced]"]' => [
                        'checked' => FALSE,
                    ],
                ],
            ],
        ];
    }
    $elements['advanced'] = [
        '#type' => 'checkbox',
        '#title' => $this->t('Advanced Mode'),
        '#default_value' => $this->getSetting('advanced'),
        '#description' => t('Switch to advanced mode and build the css yourself.'),
    ];
    $elements['css'] = [
        '#type' => 'textarea',
        '#title' => $this->t('CSS'),
        '#default_value' => $this->getSetting('css'),
        '#description' => t('Create the css statement yourself. This lets you for example, control multiple element aspects at once. You can use tokens as shown below.'),
        '#states' => [
            'visible' => [
                ':input[name="fields[' . $this->fieldDefinition
                    ->getName() . '][settings_edit_form][settings][advanced]"]' => [
                    'checked' => TRUE,
                ],
            ],
        ],
        '#element_validate' => [
            'token_element_validate',
        ],
        '#token_types' => [
            $this->getTokenType(),
            'color_field',
        ],
    ];
    $elements['token_help'] = [
        '#theme' => 'token_tree_link',
        '#token_types' => [
            $this->getTokenType(),
            'color_field',
        ],
    ];
    return $elements;
}