You are here

public function SocialMediaAdminForm::buildForm in Social media share 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides ConfigFormBase::buildForm

File

src/Form/SocialMediaAdminForm.php, line 62

Class

SocialMediaAdminForm
Class SocialMediaAdminForm.

Namespace

Drupal\social_media\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $config = $this
    ->config('social_media.settings');
  $social_medias = $this
    ->getSocialMedias();
  $token_types = [
    'current_page',
  ];
  foreach ($social_medias as $key => $label) {
    $form[$key] = [
      '#type' => 'details',
      '#title' => t('@social_media settings', [
        '@social_media' => $label,
      ]),
      '#open' => TRUE,
    ];
    $form[$key][$key . '_enable'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Enable'),
      '#default_value' => $config
        ->get('social_media.' . $key . '.enable'),
      '#description' => t('Globally disabled the settings'),
    ];
    $form[$key][$key . '_api_url'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('API url'),
      '#default_value' => $config
        ->get('social_media.' . $key . '.api_url'),
    ];

    // Handle some extra help text for the Twitter service.
    if ($key == 'twitter') {
      $form[$key][$key . '_api_url']['#description'] = t('To include Hashtags format your URL like this: https://twitter.com/intent/tweet?url=[current-page:url]&text=[text to tweet]&hashtags=[comma separated list of hashtags, with no # on them]');
    }
    $form[$key]['token_browser'] = [
      '#theme' => 'token_tree_link',
      '#token_types' => $token_types,
      '#click_insert' => TRUE,
      '#dialog' => TRUE,
    ];

    // Handle some extra configuration for email service.
    if ($key == 'email') {
      $form[$key][$key . '_api_url']['#states'] = [
        'invisible' => [
          ':input[name="' . $key . '_enable_forward' . '"]' => [
            'checked' => TRUE,
          ],
        ],
      ];
      $form[$key][$key . '_enable_forward'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Enable forward instead for client email service'),
        '#default_value' => $config
          ->get('social_media.' . $key . '.enable_forward'),
        '#description' => t('If it is checked then forward email form will open as model dialog.'),
      ];
      $form[$key][$key . '_show_forward'] = [
        '#type' => 'radios',
        '#options' => [
          1 => $this
            ->t('Model dialog'),
          2 => $this
            ->t('Separate page'),
        ],
        '#title' => $this
          ->t('Choose how you want to show forward email form'),
        '#default_value' => $config
          ->get('social_media.' . $key . '.show_forward') ? $config
          ->get('social_media.' . $key . '.show_forward') : 1,
        '#states' => [
          'visible' => [
            ':input[name="' . $key . '_enable_forward' . '"]' => [
              'checked' => TRUE,
            ],
          ],
        ],
        '#description' => t('default set as dialog popup, you can change it to show in separate page'),
      ];
    }
    $form[$key][$key . '_api_event'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Event'),
      '#options' => [
        'href' => 'href',
        'onclick' => 'onclick',
      ],
      '#default_value' => $config
        ->get('social_media.' . $key . '.api_event'),
    ];
    $form[$key][$key . '_drupalSettings'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('drupalSettings variables'),
      '#default_value' => $config
        ->get('social_media.' . $key . '.drupalSettings'),
      '#description' => t('Defines different drupalSettings variable.Each settings in new line.eg:<br/>application_id|343434434<br/> you can get those variables in js.eg drupalSettings.social_media.application_id'),
    ];
    $form[$key][$key . '_library'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Drupal library'),
      '#default_value' => $config
        ->get('social_media.' . $key . '.library'),
      '#description' => t('Add drupal custom library.eg: social_media/facebook'),
    ];
    $form[$key][$key . '_text'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Link text'),
      '#default_value' => $config
        ->get('social_media.' . $key . '.text'),
      '#description' => t('Text of the link'),
    ];
    $form[$key][$key . '_default_img'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Default image'),
      '#default_value' => $config
        ->get('social_media.' . $key . '.default_img'),
      '#description' => t('If it is checked default image will be loaded. Make service name with icon name. eg:facebook_share.svg'),
    ];
    $form[$key][$key . '_img'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Alternative image link'),
      '#states' => [
        'visible' => [
          ':input[name="' . $key . '_default_img' . '"]' => [
            'checked' => FALSE,
          ],
        ],
      ],
      '#default_value' => $config
        ->get('social_media.' . $key . '.img'),
      '#description' => t('If you want to have your custom image, give image path.'),
    ];
    $form[$key][$key . '_weight'] = [
      '#type' => 'number',
      '#title' => $this
        ->t('Order of share button'),
      '#max' => 10,
      '#min' => 0,
      '#default_value' => $config
        ->get('social_media.' . $key . '.weight'),
      '#description' => t('Order of the share link to render'),
    ];
    $form[$key][$key . '_attributes'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Attributes'),
      '#default_value' => $config
        ->get('social_media.' . $key . '.attributes'),
      '#description' => t('Defines different attributes of link. Each attribute in new line.eg:<br/>target|blank<br/> class|facebook-share js-share'),
    ];
  }
  return parent::buildForm($form, $form_state);
}