You are here

class AddToAnySettingsForm in AddToAny Share Buttons 8

Configure AddToAny settings for this site.

Hierarchy

Expanded class hierarchy of AddToAnySettingsForm

1 file declares its use of AddToAnySettingsForm
addtoany.module in ./addtoany.module
Handle AddToAny integration.
1 string reference to 'AddToAnySettingsForm'
addtoany.routing.yml in ./addtoany.routing.yml
addtoany.routing.yml

File

src/Form/AddToAnySettingsForm.php, line 16

Namespace

Drupal\addtoany\Form
View source
class AddToAnySettingsForm extends ConfigFormBase {

  /**
   * Drupal\Core\Extension\ModuleHandler definition.
   *
   * @var Drupal\Core\Extension\ModuleHandler
   */
  protected $moduleHandler;

  /**
   * Constructs a AddToAnySettingsForm object.
   *
   * @param \Drupal\Core\Extension\ModuleHandler $module_handler
   *   The factory for configuration objects.
   */
  public function __construct(ModuleHandler $module_handler) {
    $this->moduleHandler = $module_handler;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('module_handler'));
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'addtoany_settings_form';
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'addtoany.settings',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    global $base_path;
    $addtoany_settings = $this
      ->config('addtoany.settings');
    $button_img = '<img src="' . $base_path . drupal_get_path('module', 'addtoany') . '/images/%s" width="%d" height="%d"%s />';
    $button_options = [
      'default' => sprintf($button_img, 'a2a_32_32.svg', 32, 32, ' class="addtoany-round-icon"'),
      'custom' => $this
        ->t('Custom button'),
      'none' => $this
        ->t('None'),
    ];
    $attributes_for_code = [
      'autocapitalize' => [
        'off',
      ],
      'autocomplete' => [
        'off',
      ],
      'autocorrect' => [
        'off',
      ],
      'spellcheck' => [
        'false',
      ],
    ];

    // Attach CSS and JS.
    $form['#attached']['library'][] = 'addtoany/addtoany.admin';
    $form['addtoany_button_settings'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Buttons'),
      '#open' => TRUE,
    ];
    $form['addtoany_button_settings']['addtoany_buttons_size'] = [
      '#type' => 'number',
      '#title' => $this
        ->t('Icon size'),
      '#field_suffix' => ' ' . $this
        ->t('pixels'),
      '#default_value' => $addtoany_settings
        ->get('buttons_size'),
      '#size' => 10,
      '#maxlength' => 3,
      '#min' => 8,
      '#max' => 999,
      '#required' => TRUE,
    ];
    $form['addtoany_button_settings']['addtoany_service_button_settings'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Service Buttons'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    ];
    $form['addtoany_button_settings']['addtoany_service_button_settings']['addtoany_additional_html'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Service Buttons HTML code'),
      '#default_value' => $addtoany_settings
        ->get('additional_html'),
      '#description' => $this
        ->t('You can add HTML code to display customized <a href="https://www.addtoany.com/buttons/customize/drupal/standalone_services" target="_blank">standalone service buttons</a> next to each universal share button. For example: <br /> <code>&lt;a class=&quot;a2a_button_facebook&quot;&gt;&lt;/a&gt;<br />&lt;a class=&quot;a2a_button_twitter&quot;&gt;&lt;/a&gt;<br />&lt;a class=&quot;a2a_button_pinterest&quot;&gt;&lt;/a&gt;</code>
      '),
      '#attributes' => $attributes_for_code,
    ];
    $form['addtoany_button_settings']['universal_button'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Universal Button'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    ];
    $form['addtoany_button_settings']['universal_button']['addtoany_universal_button'] = [
      '#type' => 'radios',
      '#title' => $this
        ->t('Button'),
      '#default_value' => $addtoany_settings
        ->get('universal_button'),
      '#attributes' => [
        'class' => [
          'addtoany-universal-button-option',
        ],
      ],
      '#options' => $button_options,
    ];
    $form['addtoany_button_settings']['universal_button']['addtoany_custom_universal_button'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Custom button URL'),
      '#default_value' => $addtoany_settings
        ->get('custom_universal_button'),
      '#description' => $this
        ->t('URL of the button image. Example: http://example.com/share.png'),
      '#states' => [
        // Show only if custom button is selected.
        'visible' => [
          ':input[name="addtoany_universal_button"]' => [
            'value' => 'custom',
          ],
        ],
      ],
    ];
    $form['addtoany_button_settings']['universal_button']['addtoany_universal_button_placement'] = [
      '#type' => 'radios',
      '#title' => $this
        ->t('Button placement'),
      '#default_value' => $addtoany_settings
        ->get('universal_button_placement'),
      '#options' => [
        'after' => $this
          ->t('After the service buttons'),
        'before' => $this
          ->t('Before the service buttons'),
      ],
      '#states' => [
        // Hide when universal sharing is disabled.
        'invisible' => [
          ':input[name="addtoany_universal_button"]' => [
            'value' => 'none',
          ],
        ],
      ],
    ];
    $form['addtoany_additional_settings'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Additional options'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    ];
    $form['addtoany_additional_settings']['addtoany_additional_js'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Additional JavaScript'),
      '#default_value' => $addtoany_settings
        ->get('additional_js'),
      '#description' => $this
        ->t('You can add special JavaScript code for AddToAny. See <a href="https://www.addtoany.com/buttons/customize/drupal" target="_blank">AddToAny documentation</a>.'),
      '#attributes' => $attributes_for_code,
    ];
    $form['addtoany_additional_settings']['addtoany_additional_css'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Additional CSS'),
      '#default_value' => $addtoany_settings
        ->get('additional_css'),
      '#description' => $this
        ->t('You can add special CSS code for AddToAny. See <a href="https://www.addtoany.com/buttons/customize/drupal" target="_blank">AddToAny documentation</a>.'),
      '#attributes' => $attributes_for_code,
    ];
    if ($this->moduleHandler
      ->moduleExists('token')) {
      $form['tokens'] = [
        '#theme' => 'token_tree_link',
        '#token_types' => [
          'node',
        ],
        '#global_types' => TRUE,
        '#click_insert' => TRUE,
        '#show_restricted' => FALSE,
        '#recursion_limit' => 3,
        '#text' => $this
          ->t('Browse available tokens'),
      ];
    }
    $form['addtoany_entity_settings'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Entities'),
    ];
    $form['addtoany_entity_settings']['addtoany_entity_tip_1'] = [
      '#type' => 'html_tag',
      '#tag' => 'p',
      '#value' => $this
        ->t('AddToAny is available on the &quot;Manage display&quot; pages of enabled entities, e.g. Structure &gt; Content types &gt; Article &gt; Manage display.'),
    ];
    $entities = self::getContentEntities();

    // Allow modules to alter the entity types.
    \Drupal::moduleHandler()
      ->alter('addtoany_entity_types', $entities);

    // Whitelist the entity IDs that let us link to each bundle's Manage Display page.
    $linkableEntities = [
      'block_content',
      'comment',
      'commerce_product',
      'commerce_store',
      'contact_message',
      'media',
      'node',
      'paragraph',
    ];
    foreach ($entities as $entity) {
      $entityId = $entity
        ->id();
      $entityType = $entity
        ->getBundleEntityType();

      // Get all available bundles for the current entity.
      $bundles = \Drupal::service('entity_type.bundle.info')
        ->getBundleInfo($entityId);
      $links = [];
      foreach ($bundles as $machine_name => $bundle) {
        $label = $bundle['label'];

        // Some labels are TranslatableMarkup objects (such as the File entity).
        if ($label instanceof TranslatableMarkup) {
          $label = $label
            ->render();
        }

        // Check if Field UI module enabled.
        if ($this->moduleHandler
          ->moduleExists('field_ui')) {

          // Link to the bundle's Manage Display page if the entity ID supports the route pattern.
          if (in_array($entityId, $linkableEntities) && $entityType) {
            $links[] = Link::createFromRoute(t($label), "entity.entity_view_display.{$entityId}.default", [
              $entityType => $machine_name,
            ])
              ->toString();
          }
        }
      }
      $description = empty($links) ? '' : '( ' . implode(' | ', $links) . ' )';
      $form['addtoany_entity_settings'][$entityId] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('@entity', [
          '@entity' => $entity
            ->getLabel(),
        ]),
        '#default_value' => $addtoany_settings
          ->get("entities.{$entityId}"),
        '#description' => $description,
        '#attributes' => [
          'class' => [
            'addtoany-entity-checkbox',
          ],
        ],
      ];
    }
    $form['addtoany_entity_settings']['addtoany_entity_tip_2'] = [
      '#type' => 'html_tag',
      '#tag' => 'p',
      '#value' => $this
        ->t('A cache rebuild may be required before changes take effect.'),
    ];
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $values = $form_state
      ->getValues();
    $this
      ->config('addtoany.settings')
      ->set('additional_css', $values['addtoany_additional_css'])
      ->set('additional_html', $values['addtoany_additional_html'])
      ->set('additional_js', $values['addtoany_additional_js'])
      ->set('buttons_size', $values['addtoany_buttons_size'])
      ->set('custom_universal_button', $values['addtoany_custom_universal_button'])
      ->set('universal_button', $values['addtoany_universal_button'])
      ->set('universal_button_placement', $values['addtoany_universal_button_placement']);
    foreach (self::getContentEntities() as $entity) {
      $entityId = $entity
        ->id();
      $this
        ->config('addtoany.settings')
        ->set("entities.{$entityId}", $values[$entityId]);
    }
    $this
      ->config('addtoany.settings')
      ->save();
    parent::submitForm($form, $form_state);
  }

  /**
   * Get all available content entities in the environment.
   * @return array
   */
  public static function getContentEntities() {
    $content_entity_types = [];
    $entity_type_definitions = \Drupal::entityTypeManager()
      ->getDefinitions();

    /* @var $definition EntityTypeInterface */
    foreach ($entity_type_definitions as $definition) {
      if ($definition instanceof ContentEntityType) {
        $content_entity_types[] = $definition;
      }
    }
    return $content_entity_types;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AddToAnySettingsForm::$moduleHandler protected property Drupal\Core\Extension\ModuleHandler definition.
AddToAnySettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
AddToAnySettingsForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
AddToAnySettingsForm::getContentEntities public static function Get all available content entities in the environment.
AddToAnySettingsForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
AddToAnySettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
AddToAnySettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
AddToAnySettingsForm::__construct public function Constructs a AddToAnySettingsForm object. Overrides ConfigFormBase::__construct
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.