You are here

public function RRSSBSettingsForm::form in Ridiculously Responsive Social Sharing Buttons 8.2

Gets the actual form array to be built.

Overrides EntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

src/Form/RRSSBSettingsForm.php, line 18

Class

RRSSBSettingsForm
RRSSB settings form.

Namespace

Drupal\rrssb\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $all_buttons = rrssb_button_config();
  $form = parent::form($form, $form_state);
  $config = $this->entity;
  $chosen = $config
    ->get('chosen');
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Label'),
    '#default_value' => $config
      ->label(),
    '#required' => TRUE,
    '#description' => $this
      ->t('Administrative label for this button set.'),
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#machine_name' => [
      'exists' => [
        $this,
        'exists',
      ],
    ],
    '#default_value' => $config
      ->id(),
    '#disabled' => !$config
      ->isNew(),
    '#required' => TRUE,
  ];
  $form['follow'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Select type of buttons'),
    '#options' => [
      0 => $this
        ->t('Share'),
      1 => $this
        ->t('Follow'),
    ],
    '#default_value' => $config
      ->get('follow'),
    '#description' => $this
      ->t('"Share" buttons invite the visitor to share the page from your site onto their page/channel/profile.  "Follow" buttons direct the visitor to your page/channel/profile.'),
  ];

  // Create the config for the table of buttons.
  $form['chosen'] = [
    '#type' => 'table',
    '#header' => [
      $this
        ->t('Button'),
      $this
        ->t('Enabled'),
      $this
        ->t('Username'),
      $this
        ->t('Weight'),
    ],
    '#empty' => $this
      ->t('No buttons found'),
    '#tabledrag' => [
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'item-row-weight',
      ],
    ],
  ];
  foreach ($all_buttons as $name => $button) {
    $form['chosen'][$name]['#attributes']['class'][] = 'draggable';

    // Determine if this button requires a particular value of follow
    // to be valid. This is the case if one or other of the URL as not
    // present.
    // Both URLs absent makes no sense and would be a bug.
    unset($require_follow);
    if (!isset($button['follow_url'])) {
      $require_follow = 0;
    }
    elseif (!isset($button['share_url'])) {
      $require_follow = 1;
    }
    $form['chosen'][$name]['label'] = [
      '#type' => 'item',
      '#markup' => $name,
    ];
    $form['chosen'][$name]['enabled'] = [
      '#type' => 'checkbox',
      '#default_value' => isset($chosen[$name]['enabled']) ? $chosen[$name]['enabled'] : FALSE,
    ];
    if (isset($require_follow)) {

      // Hide entries where there is no corresponding URL.
      $form['chosen'][$name]['enabled']['#states'] = [
        'visible' => [
          ":input[name='follow']" => [
            'value' => $require_follow,
          ],
        ],
      ];
    }
    if (isset($button['follow_url']) && strpos($button['follow_url'], '[rrssb:username]') !== FALSE) {
      $form['chosen'][$name]['username'] = [
        '#type' => 'textfield',
        '#default_value' => isset($chosen[$name]['username']) ? $chosen[$name]['username'] : '',
        // Hide the username for share URLs where it isn't needed.
        // Otherwise it is a required field.
        '#states' => [
          'visible' => [
            ":input[name='follow']" => [
              'value' => 1,
            ],
          ],
          'required' => [
            ":input[name='follow']" => [
              'value' => 1,
            ],
            ":input[name='chosen[{$name}][enabled]']" => [
              'checked' => TRUE,
            ],
          ],
        ],
      ];
    }
    else {
      $form['chosen'][$name]['username'] = [];
    }
    $form['chosen'][$name]['weight'] = [
      '#type' => 'weight',
      '#title' => $this
        ->t('Weight for @title', [
        '@title' => $name,
      ]),
      '#title_display' => 'invisible',
      '#default_value' => isset($chosen[$name]['weight']) ? $chosen[$name]['weight'] : 0,
      '#delta' => 20,
      '#attributes' => [
        'class' => [
          'item-row-weight',
        ],
      ],
    ];
  }

  // Appearance settings stored as an array ready to pass to the library code.
  $appearance = $config
    ->get('appearance');
  $form['appearance'] = [
    '#type' => 'fieldset',
    '#tree' => TRUE,
    '#title' => $this
      ->t('Appearance'),
  ];
  $form['appearance']['size'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Size'),
    '#size' => 5,
    '#default_value' => $appearance['size'],
    '#description' => $this
      ->t('Size, as a proportion of default size set in CSS.'),
  ];
  $form['appearance']['shrink'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Minimum size'),
    '#size' => 5,
    '#default_value' => $appearance['shrink'],
    '#description' => $this
      ->t('Minimum size to shrink buttons to, as a proportion of original size.'),
  ];
  $form['appearance']['regrow'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Extra row size'),
    '#size' => 5,
    '#default_value' => $appearance['regrow'],
    '#description' => $this
      ->t('Maximum size of buttons after they have been forced to split onto extra rows of buttons, as a proportion of original size.'),
  ];
  $form['appearance']['minRows'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Minimum rows'),
    '#size' => 5,
    '#default_value' => $appearance['minRows'],
    '#description' => $this
      ->t('Minimum number of rows of buttons.  Set to a large value to create vertical layout.'),
  ];
  $form['appearance']['maxRows'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Maximum rows'),
    '#size' => 5,
    '#default_value' => $appearance['maxRows'],
    '#description' => $this
      ->t('Maximum number of rows of buttons.  If more rows would be needed, instead the labels are hidden.  Set to a large value to keep labels if at all possible.'),
  ];
  $form['appearance']['prefixReserve'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Prefix reserved width'),
    '#size' => 5,
    '#default_value' => $appearance['prefixReserve'],
    '#description' => $this
      ->t('Proportion of total width reserved for prefix to be inline.'),
  ];
  $form['appearance']['prefixHide'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Prefix maximum width'),
    '#size' => 5,
    '#default_value' => $appearance['prefixHide'],
    '#description' => $this
      ->t('Maximum prefix width as a proportion of total width before hiding prefix.'),
  ];
  $form['appearance']['alignRight'] = [
    '#type' => 'checkbox',
    '#title' => t('Right-align buttons'),
    '#size' => 5,
    '#default_value' => $appearance['alignRight'],
    '#description' => t('By default, buttons are left-aligned, with any padding added on the right.  Enable this to right-align, and instead pad on the left.'),
  ];
  $form['prefix'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Prefix text before the buttons'),
    '#default_value' => $config
      ->get('prefix'),
    '#description' => $this
      ->t('Put this text before the buttons.  For example "Follow us" or "Share this page".'),
  ];
  $form['image_tokens'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Tokens to use to find images'),
    '#default_value' => $config
      ->get('image_tokens'),
    '#description' => $this
      ->t('Enter one or more tokens, separated by |.  These tokens will be tried in turn to determine the image to use in buttons.
        The default value is @default which you can adapt to pick other fields or as desired.', [
      '@default' => RRSSB_DEFAULT_IMAGE_TOKEN,
    ]),
  ];
  return $form;
}