Skip to content

Instantly share code, notes, and snippets.

@mrkmiller
Last active January 9, 2019 19:07
Show Gist options
  • Select an option

  • Save mrkmiller/14045caeb78e32ff984c8f0ac1e77492 to your computer and use it in GitHub Desktop.

Select an option

Save mrkmiller/14045caeb78e32ff984c8f0ac1e77492 to your computer and use it in GitHub Desktop.
Focal Link Icon block style plugin. Replace "custom_theme" with the name of your theme and add fontawsome to the library.
focal_link_icon:
label: 'Icon Selection'
class: '\Drupal\custome_theme\Plugin\BlockStyle\FocalLinkIcon'
include:
- 'sf_focal_link'
<?php
namespace Drupal\custom_theme\Plugin\BlockStyle;
use Drupal\block_style_plugins\Plugin\BlockStyleBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Provides a 'FocalLinkIcon' style.
*/
class FocalLinkIcon extends BlockStyleBase {
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return ['focal_link_icon' => ''];
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['focal_link_icon'] = [
'#type' => 'textfield',
'#title' => $this->t('Icon Classes'),
'#description' => $this->t('<a href="https://fontawesome.com/icons?d=gallery&m=free" target="_blank">Choose an Icon</a> and paste in the Font Awesome classes "<strong>fas fa-check</strong>" or markup "<strong>&lt;i class=&quot;fas fa-check&quot;&gt;&lt;/i&gt;</strong>".'),
'#default_value' => $this->configuration['focal_link_icon'],
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$icon = $form_state->getValue('focal_link_icon');
if ($icon) {
$icon = $this->stripIconMarkup($icon);
$form_state->setValue('focal_link_icon', $icon);
}
}
/**
* Remove Font Awesome icon markup to return only classes.
*
* @param string $icon
* The font awesome icon markup.
*
* @return string
* The font awesome classes.
*/
protected function stripIconMarkup($icon) {
preg_match('/class="([^"]+)"/', $icon, $matches);
if (isset($matches[1])) {
return $matches[1];
}
else {
return $icon;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment