Loading config/schema/micon.schema.yml +0 −3 Original line number Diff line number Diff line Loading @@ -19,6 +19,3 @@ micon.micon.*: archive: type: sequence label: 'Archive' static_archive_location: type: string label: "Static archive location" micon.module +1 −6 Original line number Diff line number Diff line Loading @@ -110,12 +110,7 @@ function micon_library_info_build() { $libraries = []; foreach (Micon::loadMultiple() as $micon) { if ($stylesheet = $micon->getStylesheet()) { if ($micon->hasStaticArchive()) { $libraries['micon.' . $micon->id()]['css']['theme']['/' . $stylesheet] = []; } else { $libraries['micon.' . $micon->id()]['css']['theme'][$stylesheet] = []; } // Add SVG library if necessary. if ($micon->type() == 'image') { $libraries['micon.' . $micon->id()]['dependencies'][] = 'micon/micon.svg'; Loading micon.theme.inc +1 −1 Original line number Diff line number Diff line Loading @@ -103,7 +103,7 @@ function micon_template_preprocess_micon_package__font(&$variables) { $variables['content']['use']['#children']['details'] = [ '#markup' => '<textarea rows="13" class="micon-usage"> /* use !important to prevent issues with browser extensions that change fonts */ font-family: \'' . (isset($info['metadata']['name']) ? $info['metadata']['name'] : '') . '\' !important; font-family: \'' . $info['metadata']['name'] . '\' !important; speak: none; font-style: normal; font-weight: normal; Loading src/Entity/Micon.php +31 −99 Original line number Diff line number Diff line Loading @@ -7,7 +7,6 @@ use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Entity\EntityMalformedException; use Drupal\Component\Serialization\Json; use Drupal\micon\MiconIcon; use const DRUPAL_ROOT; /** * Defines the Micon entity. Loading Loading @@ -39,8 +38,7 @@ use const DRUPAL_ROOT; * "id" = "id", * "label" = "label", * "uuid" = "uuid", * "status" = "status", * "static_archive_location" = "static_archive_location", * "status" = "status" * }, * links = { * "canonical" = "/admin/structure/micon/{micon}", Loading @@ -52,7 +50,6 @@ use const DRUPAL_ROOT; * ) */ class Micon extends ConfigEntityBase implements MiconInterface { const STATIC_ARCHIVE_LOCATION = 'static_archive_location'; /** * The Micon ID. Loading Loading @@ -114,35 +111,15 @@ class Micon extends ConfigEntityBase implements MiconInterface { * {@inheritdoc} */ public function getInfo() { if (!empty($this->info)) { return $this->info; } if (empty($this->info)) { $this->info = []; $path = $this->getDirectory() . '/selection.json'; if (!file_exists($path) && $this->hasStaticArchive()) { $this->archiveExtract(DRUPAL_ROOT . '/' . $this->get(static::STATIC_ARCHIVE_LOCATION)); } if (file_exists($path)) { $data = file_get_contents($path); $this->info = Json::decode($data); } return $this->info; } /** * Is a static archive location defined. * * @return bool * Does this have a static archive location defined? */ public function hasStaticArchive() { $static_archive = $this->get(static::STATIC_ARCHIVE_LOCATION); return !empty($static_archive); return $this->info; } /** Loading Loading @@ -214,13 +191,8 @@ class Micon extends ConfigEntityBase implements MiconInterface { * The unique path to the package directory. */ protected function getDirectory() { if ($this->hasStaticArchive()) { return $this->get(static::STATIC_ARCHIVE_LOCATION); } else { return $this->directory . '/' . $this->id(); } } /** * {@inheritdoc} Loading Loading @@ -258,14 +230,10 @@ class Micon extends ConfigEntityBase implements MiconInterface { $original = $storage->loadUnchanged($this->getOriginalId()); } if ($this->hasStaticArchive()) { $this->set('archive', ''); } if (!$this->get('archive') && !$this->hasStaticArchive()) { if (!$this->get('archive')) { throw new EntityMalformedException('IcoMoon icon package is required.'); } if (!$this->hasStaticArchive() && ($this->isNew() || $original->get('archive') !== $this->get('archive'))) { if ($this->isNew() || $original->get('archive') !== $this->get('archive')) { $this->archiveDecode(); } } Loading @@ -276,49 +244,21 @@ class Micon extends ConfigEntityBase implements MiconInterface { public static function preDelete(EntityStorageInterface $storage, array $entities) { parent::preDelete($storage, $entities); foreach ($entities as $entity) { if (!$entity->hasStaticArchive()) { // Remove all files within package directory. file_unmanaged_delete_recursive($entity->getDirectory()); // Clean up empty directory. Will fail silently if it is not empty. @rmdir($entity->directory); } } } /** * Get the archive path. * * @return string * The path. */ protected function getArchivePath() { $tempfile = 'temporary://' . $this->id . '.zip'; if ($this->hasStaticArchive()) { \copy(DRUPAL_ROOT . '/' . $this->get(static::STATIC_ARCHIVE_LOCATION), $tempfile); return $tempfile; } $this->fillArchivePathFromArchiveData($tempfile); return $tempfile; } /** * Create the archive path from base64 archive data. * * @param string $path * The path to write to. */ protected function fillArchivePathFromArchiveData($path) { file_put_contents($path, $this->getArchive()); } /** * Take base64 encoded archive and save it to a temporary file for extraction. */ protected function archiveDecode() { $this->archiveExtract($this->getArchivePath()); $data = $this->getArchive(); $zip_path = 'temporary://' . $this->id() . '.zip'; file_put_contents($zip_path, $data); $this->archiveExtract($zip_path); } /** Loading Loading @@ -367,26 +307,18 @@ class Micon extends ConfigEntityBase implements MiconInterface { // Update IcoMoon selection.json. $file_path = $directory . '/selection.json'; $file_contents = file_get_contents($file_path); $replacementMap = [ // Protect icon keys. '"icons":' => 'MICONSIcons', '"icon":' => 'MICONIcon', 'iconIdx' => 'MICONIdx', $this->getPrefix() => 'MICONPrefix', $file_contents = str_replace('"icons":', 'MICONSIcons', $file_contents); $file_contents = str_replace('"icon":', 'MICONIcon', $file_contents); $file_contents = str_replace('iconIdx', 'MICONIdx', $file_contents); $file_contents = str_replace($this->getPrefix(), 'MICONPrefix', $file_contents); // The name and selector should be updated to match entity info. $this->getName() => $this->id(), $file_contents = str_replace($this->getName(), $this->id(), $file_contents); // Return protected keys. 'MICONSIcons' => '"icons":', 'MICONIcon' => '"icon":', 'MICONIdx' => 'iconIdx', 'MICONPrefix' => $this->id() . '-', ]; foreach ($replacementMap as $search => $replace) { $file_contents = str_replace($search, $replace, $file_contents); } $file_contents = str_replace('MICONSIcons', '"icons":', $file_contents); $file_contents = str_replace('MICONIcon', '"icon":', $file_contents); $file_contents = str_replace('MICONIdx', 'iconIdx', $file_contents); $file_contents = str_replace('MICONPrefix', $this->id() . '-', $file_contents); file_put_contents($file_path, $file_contents); // Update IcoMoon stylesheet. Loading src/Form/MiconForm.php +1 −49 Original line number Diff line number Diff line Loading @@ -4,7 +4,6 @@ namespace Drupal\micon\Form; use Drupal\Core\Entity\EntityForm; use Drupal\Core\Form\FormStateInterface; use Drupal\micon\Entity\Micon; use Drupal\micon\Entity\MiconInterface; /** Loading @@ -20,7 +19,6 @@ class MiconForm extends EntityForm { public function form(array $form, FormStateInterface $form_state) { $form = parent::form($form, $form_state); /* @var $micon \Drupal\micon\Entity\Micon */ $micon = $this->entity; $form['label'] = [ '#type' => 'textfield', Loading Loading @@ -51,22 +49,6 @@ class MiconForm extends EntityForm { 'file_validate_extensions' => array('zip'), 'file_validate_size' => array(file_upload_max_size()), ); $fileOrStatic = isset($form_state->getUserInput()['file_or_static']) ? $form_state->getUserInput()['file_or_static'] : ''; if ($fileOrStatic === 'file') { $micon->setStaticArchiveLocation(''); } $form['file_or_static'] = array( '#title' => t('Upload file or use a static location'), '#type' => 'select', '#options' => [ 'file' => 'file', 'static' => 'static', ], '#default_value' => $micon->hasStaticArchive() ? 'static' : 'file', ); $form['file'] = array( '#type' => 'file', '#title' => $micon->isNew() ? $this->t('IcoMoon Font Package') : $this->t('Replace IcoMoon Font Package'), Loading @@ -78,26 +60,6 @@ class MiconForm extends EntityForm { '#size' => 50, '#upload_validators' => $validators, '#attributes' => array('class' => array('file-import-input')), '#states' => [ 'invisible' => [ 'select[name="file_or_static"]' => ['value' => 'static'], ], ], ); $form[Micon::STATIC_ARCHIVE_LOCATION] = array( '#type' => 'textfield', '#title' => t('Static archive location'), '#description' => t('Static archive location, relative from the DRUPAL_ROOT'), '#default_value' => $micon->get(Micon::STATIC_ARCHIVE_LOCATION), '#states' => [ 'invisible' => [ 'select[name="file_or_static"]' => ['value' => 'file'], ], 'disabled' => [ 'select[name="file_or_static"]' => ['value' => 'file'], ], ], ); $form['#entity_builders']['update_status'] = [$this, 'updateStatus']; Loading Loading @@ -186,10 +148,6 @@ class MiconForm extends EntityForm { public function validateForm(array &$form, FormStateInterface $form_state) { parent::validateForm($form, $form_state); if ($form_state->getUserInput()[Micon::STATIC_ARCHIVE_LOCATION] !== '') { return; } $this->file = file_save_upload('file', $form['file']['#upload_validators'], FALSE, 0); // Ensure we have the file uploaded. Loading @@ -204,13 +162,7 @@ class MiconForm extends EntityForm { public function save(array $form, FormStateInterface $form_state) { $micon = $this->entity; $micon->set(Micon::STATIC_ARCHIVE_LOCATION, isset($form_state->getUserInput()[Micon::STATIC_ARCHIVE_LOCATION]) ? $form_state->getUserInput()[Micon::STATIC_ARCHIVE_LOCATION] : NULL ); if (isset($this->file)) { if ($this->file) { try { $zip_path = $this->file->getFileUri(); $micon->setArchive($zip_path); Loading Loading
config/schema/micon.schema.yml +0 −3 Original line number Diff line number Diff line Loading @@ -19,6 +19,3 @@ micon.micon.*: archive: type: sequence label: 'Archive' static_archive_location: type: string label: "Static archive location"
micon.module +1 −6 Original line number Diff line number Diff line Loading @@ -110,12 +110,7 @@ function micon_library_info_build() { $libraries = []; foreach (Micon::loadMultiple() as $micon) { if ($stylesheet = $micon->getStylesheet()) { if ($micon->hasStaticArchive()) { $libraries['micon.' . $micon->id()]['css']['theme']['/' . $stylesheet] = []; } else { $libraries['micon.' . $micon->id()]['css']['theme'][$stylesheet] = []; } // Add SVG library if necessary. if ($micon->type() == 'image') { $libraries['micon.' . $micon->id()]['dependencies'][] = 'micon/micon.svg'; Loading
micon.theme.inc +1 −1 Original line number Diff line number Diff line Loading @@ -103,7 +103,7 @@ function micon_template_preprocess_micon_package__font(&$variables) { $variables['content']['use']['#children']['details'] = [ '#markup' => '<textarea rows="13" class="micon-usage"> /* use !important to prevent issues with browser extensions that change fonts */ font-family: \'' . (isset($info['metadata']['name']) ? $info['metadata']['name'] : '') . '\' !important; font-family: \'' . $info['metadata']['name'] . '\' !important; speak: none; font-style: normal; font-weight: normal; Loading
src/Entity/Micon.php +31 −99 Original line number Diff line number Diff line Loading @@ -7,7 +7,6 @@ use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Entity\EntityMalformedException; use Drupal\Component\Serialization\Json; use Drupal\micon\MiconIcon; use const DRUPAL_ROOT; /** * Defines the Micon entity. Loading Loading @@ -39,8 +38,7 @@ use const DRUPAL_ROOT; * "id" = "id", * "label" = "label", * "uuid" = "uuid", * "status" = "status", * "static_archive_location" = "static_archive_location", * "status" = "status" * }, * links = { * "canonical" = "/admin/structure/micon/{micon}", Loading @@ -52,7 +50,6 @@ use const DRUPAL_ROOT; * ) */ class Micon extends ConfigEntityBase implements MiconInterface { const STATIC_ARCHIVE_LOCATION = 'static_archive_location'; /** * The Micon ID. Loading Loading @@ -114,35 +111,15 @@ class Micon extends ConfigEntityBase implements MiconInterface { * {@inheritdoc} */ public function getInfo() { if (!empty($this->info)) { return $this->info; } if (empty($this->info)) { $this->info = []; $path = $this->getDirectory() . '/selection.json'; if (!file_exists($path) && $this->hasStaticArchive()) { $this->archiveExtract(DRUPAL_ROOT . '/' . $this->get(static::STATIC_ARCHIVE_LOCATION)); } if (file_exists($path)) { $data = file_get_contents($path); $this->info = Json::decode($data); } return $this->info; } /** * Is a static archive location defined. * * @return bool * Does this have a static archive location defined? */ public function hasStaticArchive() { $static_archive = $this->get(static::STATIC_ARCHIVE_LOCATION); return !empty($static_archive); return $this->info; } /** Loading Loading @@ -214,13 +191,8 @@ class Micon extends ConfigEntityBase implements MiconInterface { * The unique path to the package directory. */ protected function getDirectory() { if ($this->hasStaticArchive()) { return $this->get(static::STATIC_ARCHIVE_LOCATION); } else { return $this->directory . '/' . $this->id(); } } /** * {@inheritdoc} Loading Loading @@ -258,14 +230,10 @@ class Micon extends ConfigEntityBase implements MiconInterface { $original = $storage->loadUnchanged($this->getOriginalId()); } if ($this->hasStaticArchive()) { $this->set('archive', ''); } if (!$this->get('archive') && !$this->hasStaticArchive()) { if (!$this->get('archive')) { throw new EntityMalformedException('IcoMoon icon package is required.'); } if (!$this->hasStaticArchive() && ($this->isNew() || $original->get('archive') !== $this->get('archive'))) { if ($this->isNew() || $original->get('archive') !== $this->get('archive')) { $this->archiveDecode(); } } Loading @@ -276,49 +244,21 @@ class Micon extends ConfigEntityBase implements MiconInterface { public static function preDelete(EntityStorageInterface $storage, array $entities) { parent::preDelete($storage, $entities); foreach ($entities as $entity) { if (!$entity->hasStaticArchive()) { // Remove all files within package directory. file_unmanaged_delete_recursive($entity->getDirectory()); // Clean up empty directory. Will fail silently if it is not empty. @rmdir($entity->directory); } } } /** * Get the archive path. * * @return string * The path. */ protected function getArchivePath() { $tempfile = 'temporary://' . $this->id . '.zip'; if ($this->hasStaticArchive()) { \copy(DRUPAL_ROOT . '/' . $this->get(static::STATIC_ARCHIVE_LOCATION), $tempfile); return $tempfile; } $this->fillArchivePathFromArchiveData($tempfile); return $tempfile; } /** * Create the archive path from base64 archive data. * * @param string $path * The path to write to. */ protected function fillArchivePathFromArchiveData($path) { file_put_contents($path, $this->getArchive()); } /** * Take base64 encoded archive and save it to a temporary file for extraction. */ protected function archiveDecode() { $this->archiveExtract($this->getArchivePath()); $data = $this->getArchive(); $zip_path = 'temporary://' . $this->id() . '.zip'; file_put_contents($zip_path, $data); $this->archiveExtract($zip_path); } /** Loading Loading @@ -367,26 +307,18 @@ class Micon extends ConfigEntityBase implements MiconInterface { // Update IcoMoon selection.json. $file_path = $directory . '/selection.json'; $file_contents = file_get_contents($file_path); $replacementMap = [ // Protect icon keys. '"icons":' => 'MICONSIcons', '"icon":' => 'MICONIcon', 'iconIdx' => 'MICONIdx', $this->getPrefix() => 'MICONPrefix', $file_contents = str_replace('"icons":', 'MICONSIcons', $file_contents); $file_contents = str_replace('"icon":', 'MICONIcon', $file_contents); $file_contents = str_replace('iconIdx', 'MICONIdx', $file_contents); $file_contents = str_replace($this->getPrefix(), 'MICONPrefix', $file_contents); // The name and selector should be updated to match entity info. $this->getName() => $this->id(), $file_contents = str_replace($this->getName(), $this->id(), $file_contents); // Return protected keys. 'MICONSIcons' => '"icons":', 'MICONIcon' => '"icon":', 'MICONIdx' => 'iconIdx', 'MICONPrefix' => $this->id() . '-', ]; foreach ($replacementMap as $search => $replace) { $file_contents = str_replace($search, $replace, $file_contents); } $file_contents = str_replace('MICONSIcons', '"icons":', $file_contents); $file_contents = str_replace('MICONIcon', '"icon":', $file_contents); $file_contents = str_replace('MICONIdx', 'iconIdx', $file_contents); $file_contents = str_replace('MICONPrefix', $this->id() . '-', $file_contents); file_put_contents($file_path, $file_contents); // Update IcoMoon stylesheet. Loading
src/Form/MiconForm.php +1 −49 Original line number Diff line number Diff line Loading @@ -4,7 +4,6 @@ namespace Drupal\micon\Form; use Drupal\Core\Entity\EntityForm; use Drupal\Core\Form\FormStateInterface; use Drupal\micon\Entity\Micon; use Drupal\micon\Entity\MiconInterface; /** Loading @@ -20,7 +19,6 @@ class MiconForm extends EntityForm { public function form(array $form, FormStateInterface $form_state) { $form = parent::form($form, $form_state); /* @var $micon \Drupal\micon\Entity\Micon */ $micon = $this->entity; $form['label'] = [ '#type' => 'textfield', Loading Loading @@ -51,22 +49,6 @@ class MiconForm extends EntityForm { 'file_validate_extensions' => array('zip'), 'file_validate_size' => array(file_upload_max_size()), ); $fileOrStatic = isset($form_state->getUserInput()['file_or_static']) ? $form_state->getUserInput()['file_or_static'] : ''; if ($fileOrStatic === 'file') { $micon->setStaticArchiveLocation(''); } $form['file_or_static'] = array( '#title' => t('Upload file or use a static location'), '#type' => 'select', '#options' => [ 'file' => 'file', 'static' => 'static', ], '#default_value' => $micon->hasStaticArchive() ? 'static' : 'file', ); $form['file'] = array( '#type' => 'file', '#title' => $micon->isNew() ? $this->t('IcoMoon Font Package') : $this->t('Replace IcoMoon Font Package'), Loading @@ -78,26 +60,6 @@ class MiconForm extends EntityForm { '#size' => 50, '#upload_validators' => $validators, '#attributes' => array('class' => array('file-import-input')), '#states' => [ 'invisible' => [ 'select[name="file_or_static"]' => ['value' => 'static'], ], ], ); $form[Micon::STATIC_ARCHIVE_LOCATION] = array( '#type' => 'textfield', '#title' => t('Static archive location'), '#description' => t('Static archive location, relative from the DRUPAL_ROOT'), '#default_value' => $micon->get(Micon::STATIC_ARCHIVE_LOCATION), '#states' => [ 'invisible' => [ 'select[name="file_or_static"]' => ['value' => 'file'], ], 'disabled' => [ 'select[name="file_or_static"]' => ['value' => 'file'], ], ], ); $form['#entity_builders']['update_status'] = [$this, 'updateStatus']; Loading Loading @@ -186,10 +148,6 @@ class MiconForm extends EntityForm { public function validateForm(array &$form, FormStateInterface $form_state) { parent::validateForm($form, $form_state); if ($form_state->getUserInput()[Micon::STATIC_ARCHIVE_LOCATION] !== '') { return; } $this->file = file_save_upload('file', $form['file']['#upload_validators'], FALSE, 0); // Ensure we have the file uploaded. Loading @@ -204,13 +162,7 @@ class MiconForm extends EntityForm { public function save(array $form, FormStateInterface $form_state) { $micon = $this->entity; $micon->set(Micon::STATIC_ARCHIVE_LOCATION, isset($form_state->getUserInput()[Micon::STATIC_ARCHIVE_LOCATION]) ? $form_state->getUserInput()[Micon::STATIC_ARCHIVE_LOCATION] : NULL ); if (isset($this->file)) { if ($this->file) { try { $zip_path = $this->file->getFileUri(); $micon->setArchive($zip_path); Loading