View source
<?php
namespace Drupal\potx\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Extension\ThemeHandlerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
class PotxExtractTranslationForm extends FormBase {
protected $languageManager;
protected $moduleHandler;
protected $themeHandler;
public function __construct(LanguageManagerInterface $language_manager, ModuleHandlerInterface $module_handler, ThemeHandlerInterface $theme_handler) {
$this->languageManager = $language_manager;
$this->moduleHandler = $module_handler;
$this->themeHandler = $theme_handler;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('language_manager'), $container
->get('module_handler'), $container
->get('theme_handler'));
}
public function getFormId() {
return 'potx_extract_transation';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$components = $this
->generateComponentList();
$this
->buildComponentSelector($form, $components);
$languages = $this->languageManager
->getLanguages();
if (count($languages) > 1 || !isset($languages['en'])) {
$options = [
'n/a' => $this
->t('Language independent template'),
];
foreach ($languages as $langcode => $language) {
if ($langcode == 'en') {
continue;
}
$options[$langcode] = $this
->t('Template file for @langname translations', [
'@langname' => $language
->getName(),
]);
}
$form['langcode'] = [
'#type' => 'radios',
'#title' => $this
->t('Template language'),
'#default_value' => 'n/a',
'#options' => $options,
'#description' => $this
->t('Export a language independent or language dependent (plural forms, language team name, etc.) template.'),
];
$form['translations'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Include translations'),
'#description' => $this
->t('Include translations of strings in the file generated. Not applicable for language independent templates.'),
];
}
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#button_type' => 'primary',
'#value' => $this
->t('Extract'),
];
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
if (empty($form_state
->getValue('component'))) {
$form_state
->setErrorByName('component', $this
->t('You should select a component to export.'));
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
@set_time_limit(0);
$this->moduleHandler
->loadInclude('potx', 'inc');
$this->moduleHandler
->loadInclude('potx', 'inc', 'potx.local');
potx_status('set', POTX_STATUS_MESSAGE);
$component = $form_state
->getValue('component');
$pathinfo = pathinfo($component);
if (!isset($pathinfo['filename'])) {
$pathinfo['filename'] = substr($pathinfo['basename'], 0, strrpos($pathinfo['basename'], '.'));
}
if (isset($pathinfo['extension'])) {
potx_local_init($pathinfo['dirname']);
$files = _potx_explore_dir($pathinfo['dirname'] . '/', $pathinfo['filename']);
$strip_prefix = 1 + strlen($pathinfo['dirname']);
$outputname = $pathinfo['filename'];
}
else {
potx_local_init($component);
$files = _potx_explore_dir($component . '/');
$strip_prefix = 1 + strlen($component);
$outputname = $pathinfo['basename'];
}
$template_langcode = $translation_langcode = NULL;
if ($form_state
->hasValue('langcode') && $form_state
->getValue('langcode') != 'n/a') {
$template_langcode = $form_state
->getValue('langcode');
$outputname .= '.' . $template_langcode;
if (!empty($form_state
->getValue('translations'))) {
$translation_langcode = $template_langcode;
$outputname .= '.po';
}
else {
$outputname .= '.pot';
}
}
else {
$outputname .= '.pot';
}
foreach ($files as $file) {
_potx_process_file($file, $strip_prefix);
}
potx_finish_processing('_potx_save_string');
_potx_build_files(POTX_STRING_RUNTIME, POTX_BUILD_SINGLE, 'general', '_potx_save_string', '_potx_save_version', '_potx_get_header', $template_langcode, $translation_langcode);
_potx_write_files($outputname, 'attachment');
exit;
}
private function generateComponentList() {
$components = [];
$modules = $this->moduleHandler
->getModuleList();
$themes = $this->themeHandler
->listInfo();
$result = array_merge($modules, $themes);
foreach ($result as $component) {
$path_parts = explode('/', $component
->getPath());
$dir =& $components;
foreach ($path_parts as $dirname) {
if (!isset($dir[$dirname])) {
$dir[$dirname] = [];
}
$dir =& $dir[$dirname];
}
$dir['#' . $component
->getExtensionFilename()] = $component;
$dir['#-count'] = isset($dir['#-count']) ? $dir['#-count'] + 1 : 1;
}
return $components;
}
private function buildComponentSelector(array &$form, array &$components, $dirname = '') {
if (isset($components['#-count'])) {
$component_count = $components['#-count'];
unset($components['#-count']);
}
$dirkeys = array_keys($components);
if (isset($component_count) && count($components) == 1) {
$component = array_shift($components);
$dirname = $component
->getPath();
$form[$this
->getFormElementId('dir', $dirname)] = [
'#type' => 'radio',
'#title' => t('Extract from %name in the %directory directory', [
'%directory' => $dirname,
'%name' => $component
->getName(),
]),
'#description' => t('Generates output from all files found in this directory.'),
'#default_value' => 0,
'#return_value' => $dirname,
'#parents' => [
'component',
],
];
return;
}
if (preg_match('!/(modules|themes)\\b(/.+)?!', $dirname, $pathmatch)) {
$t_args = [
'@directory' => substr($dirname, 1),
];
if (isset($pathmatch[2])) {
$form[$this
->getFormElementId('dir', $dirname)] = [
'#type' => 'radio',
'#title' => t('Extract from all in directory "@directory"', $t_args),
'#description' => t('To extract from a single component in this directory, choose the desired entry in the fieldset below.'),
'#default_value' => 0,
'#return_value' => substr($dirname, 1),
'#parents' => [
'component',
],
];
}
$element = [
'#type' => 'details',
'#title' => t('Directory "@directory"', $t_args),
'#open' => FALSE,
];
$form[$this
->getFormElementId('fs', $dirname)] =& $element;
}
else {
$element =& $form;
}
foreach ($dirkeys as $entry) {
if ($entry[0] == '#') {
$t_args = [
'%directory' => $components[$entry]
->getPath(),
'%name' => $components[$entry]
->getName(),
'%pattern' => $components[$entry]
->getName() . '.*',
];
$element[$this
->getFormElementId('com', $components[$entry]
->getExtensionFilename())] = [
'#type' => 'radio',
'#title' => $this
->t('Extract from %name', $t_args),
'#description' => $this
->t('Extract from files named %pattern in the %directory directory.', $t_args),
'#default_value' => 0,
'#return_value' => $components[$entry]
->getExtensionPathname(),
'#parents' => [
'component',
],
];
}
else {
$this
->buildComponentSelector($element, $components[$entry], "{$dirname}/{$entry}");
}
}
return count($components);
}
private function getFormElementId($type, $path) {
return 'potx-' . $type . '-' . preg_replace('/[^a-zA-Z0-9]+/', '-', $path);
}
}