Commit de647ae2 authored by git's avatar git Committed by Daniel Moberly
Browse files

Issue #3219579 by manuel.adan, dennis_meuwissen, chrisck: Allow the additional...

Issue #3219579 by manuel.adan, dennis_meuwissen, chrisck: Allow the additional field settings form to be hidden
parent 3260a4b8
Loading
Loading
Loading
Loading
+23 −0
Changes for fontawesome.install: 23 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -92,6 +92,22 @@ function fontawesome_requirements($phase) {
  return $requirements;
}

/**
 * Implements hook_install().
 */
function fontawesome_install() {
  _fontawesome_install_grant_default_permissions();
}

/**
 * Grants 'access fontawesome additional settings' to authenticated users.
 */
function _fontawesome_install_grant_default_permissions() {
  /** @var \Drupal\user\RoleInterface $authenticated_role */
  $authenticated_role = \Drupal::entityTypeManager()->getStorage('user_role')->load('authenticated');
  $authenticated_role->grantPermission('access fontawesome additional settings')->save();
}

/**
 * Implements hook_uninstall().
 */
@@ -120,3 +136,10 @@ function fontawesome_update_8001() {
  $config->set('load_assets', TRUE);
  $config->save(TRUE);
}

/**
 * Grants new 'access fontawesome additional settings' to authenticated users.
 */
function fontawesome_update_8002() {
  _fontawesome_install_grant_default_permissions();
}
 No newline at end of file
+3 −0
Changes for fontawesome.permissions.yml: 3 added lines, 0 removed lines.
Original line number Diff line number Diff line
access fontawesome additional settings:
  title: 'Access fontawesome additional settings'
  description: 'Grants access to the additional settings in fontawesome fields type. Without this permission, only the icon can be selected.'
 No newline at end of file
+43 −52
Changes for src/Plugin/Field/FieldWidget/FontAwesomeIconWidget.php: 43 added lines, 52 removed lines.
Original line number Diff line number Diff line
@@ -11,6 +11,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Url;
use Drupal\Core\Link;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Session\AccountInterface;
use Drupal\fontawesome\FontAwesomeManagerInterface;

/**
@@ -40,14 +41,22 @@ class FontAwesomeIconWidget extends WidgetBase implements ContainerFactoryPlugin
   */
  protected $fontAwesomeManager;

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $currentUser;

  /**
   * {@inheritdoc}
   */
  public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, ConfigFactory $config_factory, FontAwesomeManagerInterface $font_awesome_manager) {
  public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, array $third_party_settings, ConfigFactory $config_factory, FontAwesomeManagerInterface $font_awesome_manager, AccountInterface $current_user) {
    parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings);

    $this->configFactory = $config_factory;
    $this->fontAwesomeManager = $font_awesome_manager;
    $this->currentUser = $current_user;
  }

  /**
@@ -61,7 +70,8 @@ class FontAwesomeIconWidget extends WidgetBase implements ContainerFactoryPlugin
      $configuration['settings'],
      $configuration['third_party_settings'],
      $container->get('config.factory'),
      $container->get('fontawesome.font_awesome_manager')
      $container->get('fontawesome.font_awesome_manager'),
      $container->get('current_user')
    );
  }

@@ -102,9 +112,29 @@ class FontAwesomeIconWidget extends WidgetBase implements ContainerFactoryPlugin
      '#type' => 'details',
      '#open' => FALSE,
      '#title' => $this->t('Additional Font Awesome Settings'),
      '#access' => $this->currentUser->hasPermission('access fontawesome additional settings'),
    ];

    // Allow user to determine style.
    $style_options = [
      'fas' => $this->t('Solid'),
      'far' => $this->t('Regular'),
      'fal' => $this->t('Light'),
      'fad' => $this->t('Duotone'),
      'fak' => $this->t('Kit Uploads'),
    ];
    if (is_bool($configuration_settings->get('use_solid_file')) && !$configuration_settings->get('use_solid_file')) {
      unset($style_options['fas']);
    }
    if (is_bool($configuration_settings->get('use_regular_file')) && !$configuration_settings->get('use_regular_file')) {
      unset($style_options['far']);
    }
    if (is_bool($configuration_settings->get('use_light_file')) && !$configuration_settings->get('use_light_file')) {
      unset($style_options['fal']);
    }
    if (is_bool($configuration_settings->get('use_duotone_file')) && !$configuration_settings->get('use_duotone_file')) {
      unset($style_options['fad']);
    }
    $element['settings']['style'] = [
      '#type' => 'select',
      '#title' => $this->t('Style'),
@@ -115,15 +145,8 @@ class FontAwesomeIconWidget extends WidgetBase implements ContainerFactoryPlugin
          ],
        ]))->toString(),
      ]),
      '#options' => [
        'fas' => $this->t('Solid'),
        'far' => $this->t('Regular'),
        'fal' => $this->t('Light'),
        'fad' => $this->t('Duotone'),
        'fat' => $this->t('Thin'),
        'fak' => $this->t('Kit Uploads'),
      ],
      '#default_value' => $items[$delta]->get('style')->getValue(),
      '#options' => $style_options,
      '#default_value' => $items[$delta]->get('style')->getValue() ?? array_keys($style_options)[0],
    ];

    // Allow user to determine size.
@@ -213,17 +236,15 @@ class FontAwesomeIconWidget extends WidgetBase implements ContainerFactoryPlugin
    ];

    // Allow user to edit duotone.
    if (is_bool($configuration_settings->get('use_duotone_file')) && $configuration_settings->get('use_duotone_file')) {
      $element['settings']['duotone'] = [
        '#type' => 'details',
        '#open' => FALSE,
        // Disable power transforms for webfonts.
        '#title' => $this->t('Duotone Settings'),
        '#description' => $this->t('Duotone provides a version of every icon in Font Awesome that has two distinct shades of color. They’re great for adding more of your brand or an illustrative quality to the icons in your project. See @duotoneLink for more information. Note that duotone only works with the Pro version of Font Awesome.', [
        '@duotoneLink' => Link::fromTextAndUrl($this->t('the Font Awesome guide to duotone'), Url::fromUri('https://fontawesome.com/how-to-use/on-the-web/styling/duotone-icons', [
          'attributes' => [
            'target' => '_blank',
          ],
        ]))->toString(),
          '@duotoneLink' => Link::fromTextAndUrl($this->t('the Font Awesome guide to duotone'), Url::fromUri('https://fontawesome.com/how-to-use/on-the-web/styling/duotone-icons'))
            ->toString(),
        ]),
      ];
      $element['settings']['duotone']['swap-opacity'] = [
@@ -240,11 +261,8 @@ class FontAwesomeIconWidget extends WidgetBase implements ContainerFactoryPlugin
        // Disable power transforms for webfonts.
        '#title' => $this->t('Layer Opacity'),
        '#description' => $this->t('By default the secondary layer in a duotone icon is set to 40% opacity (via an opacity 0.4; rule in Font Awesome’s support CSS). You can explicitly set the opacity of a duotone icon’s layer by using CSS custom properties either in your style sheets or by setting them manually below. New to custom properties? Here are some @cssLink.', [
        '@cssLink' => Link::fromTextAndUrl($this->t('places to set them'), Url::fromUri('https://fontawesome.com/how-to-use/on-the-web/styling/duotone-icons#using-in-a-project', [
          'attributes' => [
            'target' => '_blank',
          ],
        ]))->toString(),
          '@cssLink' => Link::fromTextAndUrl($this->t('places to set them'), Url::fromUri('https://fontawesome.com/how-to-use/on-the-web/styling/duotone-icons#using-in-a-project'))
            ->toString(),
        ]),
      ];
      $element['settings']['duotone']['opacity']['primary'] = [
@@ -268,11 +286,8 @@ class FontAwesomeIconWidget extends WidgetBase implements ContainerFactoryPlugin
        // Disable power transforms for webfonts.
        '#title' => $this->t('Layer Color'),
        '#description' => $this->t('Like all other Font Awesome icons, duotone icons automatically inherit CSS size and color. A duotone icon consists of a primary and secondary layer. By default, The secondary layer is given an opacity of 40% so that it appears as a lighter shade of the icon’s inherited or directly set color. Using CSS custom properties, we’ve also added some color hooks to a duotone icon’s primary and secondary layers. New to custom properties? Here are some @cssLink.', [
        '@cssLink' => Link::fromTextAndUrl($this->t('places to set them'), Url::fromUri('https://fontawesome.com/how-to-use/on-the-web/styling/duotone-icons#using-in-a-project', [
          'attributes' => [
            'target' => '_blank',
          ],
        ]))->toString(),
          '@cssLink' => Link::fromTextAndUrl($this->t('places to set them'), Url::fromUri('https://fontawesome.com/how-to-use/on-the-web/styling/duotone-icons#using-in-a-project'))
            ->toString(),
        ]),
      ];
      $element['settings']['duotone']['color']['primary'] = [
@@ -289,6 +304,7 @@ class FontAwesomeIconWidget extends WidgetBase implements ContainerFactoryPlugin
        '#default_value' => isset($iconSettings['duotone']['color']['secondary']) ? $iconSettings['duotone']['color']['secondary'] : '',
        '#description' => $this->t('Opacity of the secondary duotone layer.'),
      ];
    }

    // Allow user to add masking.
    $element['settings']['masking'] = [
@@ -333,14 +349,7 @@ class FontAwesomeIconWidget extends WidgetBase implements ContainerFactoryPlugin
          ],
        ]))->toString(),
      ]),
      '#options' => [
        'fas' => $this->t('Solid'),
        'far' => $this->t('Regular'),
        'fal' => $this->t('Light'),
        'fad' => $this->t('Duotone'),
        'fat' => $this->t('Thin'),
        'fak' => $this->t('Kit Uploads'),
      ],
      '#options' => $style_options,
      '#default_value' => isset($iconSettings['masking']['style']) ? $iconSettings['masking']['style'] : '',
    ];

@@ -480,24 +489,6 @@ class FontAwesomeIconWidget extends WidgetBase implements ContainerFactoryPlugin
      ],
    ];

    // Remove style options if they aren't being loaded.
    if (is_bool($configuration_settings->get('use_solid_file')) && !$configuration_settings->get('use_solid_file')) {
      unset($element['settings']['style']['#options']['fas']);
    }
    if (is_bool($configuration_settings->get('use_regular_file')) && !$configuration_settings->get('use_regular_file')) {
      unset($element['settings']['style']['#options']['far']);
    }
    if (is_bool($configuration_settings->get('use_light_file')) && !$configuration_settings->get('use_light_file')) {
      unset($element['settings']['style']['#options']['fal']);
    }
    if (is_bool($configuration_settings->get('use_duotone_file')) && !$configuration_settings->get('use_duotone_file')) {
      unset($element['settings']['style']['#options']['fad']);
      unset($element['settings']['duotone']);
    }
    if (is_bool($configuration_settings->get('use_thin_file')) && !$configuration_settings->get('use_thin_file')) {
      unset($element['settings']['style']['#options']['fat']);
    }

    return $element;
  }