sfc-8.x-1.3/sfc.module

sfc.module
<?php

/**
 * @file
 * Contains hooks for the Single File Components module.
 */

use Drupal\Core\Asset\AttachedAssetsInterface;
use Drupal\Core\Site\Settings;
use Drupal\Core\Url;
use Drupal\sfc\ComponentNameHelper;

/**
 * Implements hook_library_info_build().
 *
 * Defines libraries for single file components dynamically.
 */
function sfc_library_info_build() {
  $libraries = [];
  /** @var \Drupal\Component\Plugin\PluginManagerInterface $manager */
  $manager = \Drupal::service('plugin.manager.single_file_component');
  foreach ($manager->getDefinitions() as $id => $definition) {
    /** @var \Drupal\sfc\ComponentInterface $component */
    $component = $manager->createInstance($id);
    $library = $component->getLibrary();
    if (empty($library)) {
      continue;
    }
    $libraries[str_replace('sfc/', '', ComponentNameHelper::getLibraryName($component))] = $library;
  }
  return $libraries;
}

/**
 * Implements hook_css_alter().
 *
 * Ensures that real asset files are written at the last moment for components.
 */
function sfc_css_alter(&$css, AttachedAssetsInterface $assets) {
  $written = &drupal_static(__FUNCTION__, []);
  foreach ($assets->getLibraries() as $name) {
    if (ComponentNameHelper::isComponentLibrary($name)) {
      $id = ComponentNameHelper::getIdFromLibraryName($name);
      if (isset($written[$id])) {
        continue;
      }
      /** @var \Drupal\Component\Plugin\PluginManagerInterface $manager */
      $manager = \Drupal::service('plugin.manager.single_file_component');
      /** @var \Drupal\sfc\ComponentInterface $component */
      $component = $manager->createInstance($id);
      if ($component->shouldWriteAssets()) {
        $component->writeAssets();
      }
      $written[$id] = TRUE;
    }
  }
}

/**
 * Implements hook_css_alter().
 */
function sfc_js_alter(&$js, AttachedAssetsInterface $assets) {
  sfc_css_alter($js, $assets);
}

/**
 * Implements hook_theme().
 */
function sfc_theme() {
  $implementations = [];
  /** @var \Drupal\Component\Plugin\PluginManagerInterface $manager */
  $manager = \Drupal::service('plugin.manager.single_file_component');
  foreach ($manager->getDefinitions() as $id => $definition) {
    /** @var \Drupal\sfc\ComponentInterface $component */
    $component = $manager->createInstance($id);
    $id = $component->getId();
    // This allows themes to override templates provided by components.
    $implementations["sfc__$id"] = [
      'template' => ComponentNameHelper::getTemplateName($component),
      // This special path is used to identify components in the loader.
      'path' => 'sfc',
    ];
    // Handle theme hook overrides if present.
    if (isset($definition['overrides'])) {
      foreach ($definition['overrides'] as $override) {
        $implementations[$override] = $implementations["sfc__$id"];
        if (strpos($override, '__') !== FALSE) {
          $implementations[$override]['base hook'] = preg_replace('/__.*/', '', $override);
        }
      }
    }
  }
  return $implementations;
}

/**
 * Helper function to build a URL to a component action.
 *
 * @param string $component_id
 *   The component ID.
 * @param string $action
 *   The action name.
 * @param string $unique_id
 *   (optional) The unique ID of the rendered component. Useful for AJAX.
 */
function sfc_action_url($component_id, $action, $unique_id = NULL) {
  $url = new Url('sfc.action', [
    'component_id' => $component_id,
    'action' => $action,
  ]);
  if ($unique_id) {
    $url->setOption('query', ['sfc_unique_id' => $unique_id]);
  }
  return $url;
}

/**
 * Requires a file once and returns its defined variables and output.
 *
 * @param string $filename
 *   The filename.
 *
 * @return array
 *   An array containing variables defined in the file.
 *   The key "content" is special and contains the output of the file.
 */
function sfc_require($filename) {
  $cache = &drupal_static(__FUNCTION__ . $filename);
  if ($cache !== NULL) {
    return $cache;
  }
  $old_keys = array_keys(get_defined_vars());

  ob_start();
  require $filename;
  $content = ob_get_contents();
  ob_end_clean();

  $vars = get_defined_vars();
  $keys = array_keys($vars);
  $new_keys = array_diff($keys, $old_keys);
  $new_vars = array_intersect_key($vars, array_flip($new_keys));
  unset($new_vars['cache']);
  unset($new_vars['old_keys']);

  $cache = $new_vars;
  return $new_vars;
}

/**
 * Implements hook_page_attachments_alter().
 */
function sfc_page_attachments_alter(array &$attachments) {
  if (Settings::get('sfc_watch_refresh', FALSE)) {
    /** @var \Drupal\Core\File\FileUrlGenerator $url_generator */
    $url_generator = \Drupal::service('file_url_generator');
    $attachments['#attached']['library'][] = 'sfc/watch_refresh';
    $attachments['#attached']['drupalSettings']['sfc_watch_file'] = $url_generator->generate('public://sfc_watch_file.txt')->setAbsolute()->toString();
    touch('public://sfc_watch_file.txt');
  }
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc