Commit ecc15bb7 authored by Miki Stojkovic's avatar Miki Stojkovic Committed by Miki Stojkovic
Browse files

Issue #3028588 by mikispeed: Cleanup initial D8 module version

parent 0753ed42
Loading
Loading
Loading
Loading
+1 −1
Changes for composer.json: 1 added line, 1 removed line.
Original line number Diff line number Diff line
{
  "name": "drupal/cyr2lat",
  "description": "Cyr2Lat Drush 9 commands.",
  "description": "Cyrillic to Latin Transliteration module.",
  "type": "drupal-module",
  "autoload": {
    "psr-4": {
+6 −2
Changes for cyr2lat.info.yml: 6 added lines, 2 removed lines.
Original line number Diff line number Diff line
name: 'Cyr2Lat'
description: 'Cyrilic to Latin Custom Transliteration module'
description: 'Cyrillic to Latin Transliteration module.'
type: module
dependencies:
  - drupal:language
  - drupal:content_translation
package: Multilingual
core: 8.x
version: 8.x-2.0
configure: cyr2lat.config

cyr2lat.libraries.yml

deleted100644 → 0
+0 −4
Changes for cyr2lat.libraries.yml: 0 added lines, 4 removed lines.
Original line number Diff line number Diff line
cyr2lat-js:
  version: 1.0
  js:
    scripts/cyr2lat.js: { minified: true }
 No newline at end of file

cyr2lat.module

deleted100644 → 0
+0 −496
Changes for cyr2lat.module: 0 added lines, 496 removed lines.
Original line number Diff line number Diff line
<?php

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\paragraphs\Entity\Paragraph;

/**
 * Handle entity update translation update.
 *
 * @param EntityInterface $entity
 */
function cyr2lat_entity_update(EntityInterface $entity) {
  $type = $entity->getEntityTypeId();
  if ($type == 'node') {
    cyr2lat_translate_logic($entity);
  }
}

/**
 * Handle entity update translation creation.
 *
 * @param EntityInterface $entity
 */
function cyr2lat_entity_insert(EntityInterface $entity) {
  $type = $entity->getEntityTypeId();
  if ($type == 'node') {
    cyr2lat_translate_logic($entity);
  }
}

function cyr2lat_translate_logic(EntityInterface $entity) {
  $language = $entity->language();
  $languageId = $language->getId();
  $config = \Drupal::config('cyr2lat.settings');
  $selectedValues = $config->getRawData();
  $selected_array_value = [];
  $entityFieldNames = [];

  // Check if translation is deleted.
  if (isset($_POST['form_id'])) {
    if (strpos($_POST['form_id'], 'delete_form') !== FALSE) {
      // Nothing to do here.
    } else {
      // Creating the translation Serbian Latin in this case.
      if ($languageId == 'sr') {
        // Get config.
        foreach ($selectedValues as $key => $singleValue) {
          if ($singleValue == 1) {
            $key = substr($key, 14);
            if ($key != '' || $key != FALSE) {
              $selected_array_value[$key] = $key;
            }
          }
        }

        // Get entity Fields.
        $entityFields = $entity->getFields();
        $entityType = $entity->getType();
        foreach ($entityFields as $singleField) {
          $name = $singleField->getName();
          $entityFieldNames[$entityType . '_' . $name] = $entityType . '_' . $name;
        }

//        $paragraphFields = $entity->getFieldDefinitions();
//
//        foreach ($paragraphFields as $referencedField) {
//          $referenceType = $referencedField->getType();
//          if ($referenceType == 'entity_reference_revisions') {
//            $name = $referencedField->getName();
//            if (strpos($name, 'field') !== false) {
//              $names[] = $name;
//            }
//          }
//        }

//        if (!empty($names)) {
//          if (count($names) == 1) {
//            $pdata = $entity->get($names[0])->referencedEntities();
//
//          } else {
//            foreach ($names as $key => $field) {
//              $pdata = $entity->get($field)->referencedEntities();
//              $pdataParagraphFields[] = $pdata;
//            }
//          }
//        }
//
//        $pfieldNames = [];
//        if (isset($pdata) && !empty($pdata)) {
//          foreach ($pdata as $object) {
//            $pdatafields = $object->getFieldDefinitions();
//            foreach ($pdatafields as $singlepfield) {
//              $pfieldNames[] = $singlepfield->getName();
//            }
//          }
//        }

        $intersect = array_intersect($selected_array_value, $entityFieldNames);
        $hasIntersect = count($intersect);
        if ($hasIntersect > 0) {
          $id = $entity->id();
          $node = \Drupal\node\Entity\Node::load($id);
          if ($node->hasTranslation('sr-latn') === TRUE) {
            $node->removeTranslation('sr-latn');
          }
          $node_srlat = $node->addTranslation('sr-latn');
          foreach ($intersect as $intersectField) {
            $intersectField = str_replace($entityType . '_', '', $intersectField);
            $text = $entity->get($intersectField)->getValue();
            foreach ($text as $keytext => $keyvalue) {
              if (isset($keyvalue['value'])) {
                if (isset($keyvalue['format']) && isset($keyvalue['value'])) {
                  $node_srlat->set($intersectField, ['format' => $keyvalue['format'], 'value' => cir2lat($keyvalue['value'])]);
                } else {
                  $node_srlat->set($intersectField, cir2lat($keyvalue['value']));
                }
              }
              unset($keyvalue);
            }
          }
          $node_srlat->setCreatedTime($node->getCreatedTime());
          $node_srlat->save();

          $lang_alias = $node->toUrl()->toString();
          $exp_alias = explode('/', $lang_alias);

          if (isset($exp_alias[2]) && $exp_alias[2] == 'node' || isset($node_srlat->toArray()['path'][0]['alias']) && isset($node_srlat->toArray()['path'][0]['alias']) == "") {
            $patternt_type = \Drupal::service('pathauto.generator')->getPatternByEntity($entity);
            if ($patternt_type != NULL) {
              $pattern = $patternt_type->getPattern();
              $token = Drupal::token();
              $bubbleable_metadata = new BubbleableMetadata();
              $unfiltered_title = $token->replace($pattern, ['node' => $node], [], $bubbleable_metadata);
              $unfiltered_title = rtrim(strtolower($unfiltered_title));
              $unfiltered_title = _cyr2lat_latin_path($unfiltered_title);
              $unfiltered_title = str_replace(',', '', $unfiltered_title);
              $slashstart = substr($unfiltered_title, 0, 1);
              if ($slashstart != '/') {
                $filtered_title = '/' . str_replace(' ', '-', $unfiltered_title);
              } else {
                $filtered_title = str_replace(' ', '-', $unfiltered_title);
              }
              \Drupal::service('path.alias_storage')->save("/node/" . $entity->id(), $filtered_title, "sr-latn");
            }
          }
          else {
            $patternt_type = \Drupal::service('pathauto.generator')->getPatternByEntity($entity);
            if ($patternt_type != NULL) {
              $pattern = $patternt_type->getPattern();
              $token = Drupal::token();
              $bubbleable_metadata = new BubbleableMetadata();
              $unfiltered_title = $token->replace($pattern, ['node' => $node], [], $bubbleable_metadata);
              $unfiltered_title = rtrim(strtolower($unfiltered_title));
              $unfiltered_title = _cyr2lat_latin_path($unfiltered_title);
              $unfiltered_title = str_replace(',', '', $unfiltered_title);
              $slashstart = substr($unfiltered_title, 0, 1);
              if ($slashstart != '/') {
                $filtered_title = '/' . str_replace(' ', '-', $unfiltered_title);
              } else {
                $filtered_title = str_replace(' ', '-', $unfiltered_title);
              }
              \Drupal::service('path.alias_storage')->save("/node/" . $entity->id(), $filtered_title, "sr-latn");
            }
          }
        }
      }
    }
  }
}


/*
* implement hook_token_info()
*/
function cyr2lat_token_info() {
  $types['cyr2lat_token_type'] = array(
    'name' => t('Cyr2Lat token type'),
    'description' => t('Cyr2Lat token type for path auto')
  );
  $tokens['cyr2lat_title'] = array(
    'name' => t('Cyr2Lat title'),
    'description' => t('Cyr2Lat title')
  );

  return array(
    'types' => $types,
    'tokens' => array(
      'cyr2lat_token_type' => $tokens
    )
  );
}

/*
* implement hook_token()
*/
function cyr2lat_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  $replacements = array();
  if ($type == 'cyr2lat_token_type') {
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'cyr2lat_title':
          $new = _cyr2lat_node_title($data);
          $replacements[$original] = $new;
          break;
      }
    }
  }
  return $replacements;
}

/**
 * Custom function to get value for custom node title token.
 */
function _cyr2lat_node_title($data) {
  // Get node object.
  $node = $data['node'];

  // Get Title value.
  $title = $node->get('title')->value;

  // Convert to dj.
  $token = cir2lat_path($title);

  // Return token value.
  return $token;
}

function cir2lat($string) {
  $search = [
    'њ',
    'љ',
    'а',
    'б',
    'в',
    'г',
    'д',
    'ђ',
    'e',
    'ж',
    'з',
    'и',
    'ј',
    'к',
    'л',
    'м',
    'н',
    'о',
    'п',
    'р',
    'с',
    'т',
    'ћ',
    'у',
    'ф',
    'х',
    'ц',
    'ч',
    'џ',
    'ш',
    'Њ',
    'Љ',
    'А',
    'Б',
    'В',
    'Г',
    'Д',
    'Ђ',
    'Е',
    'Ж',
    'З',
    'И',
    'Ј',
    'К',
    'Л',
    'М',
    'Н',
    'О',
    'П',
    'Р',
    'С',
    'Т',
    'Ћ',
    'У',
    'Ф',
    'Х',
    'Ц',
    'Ч',
    'Џ',
    'Ш',
  ];
  $replace = [
    'nj',
    'lj',
    'a',
    'b',
    'v',
    'g',
    'd',
    'đ',
    'e',
    'ž',
    'z',
    'i',
    'j',
    'k',
    'l',
    'm',
    'n',
    'o',
    'p',
    'r',
    's',
    't',
    'ć',
    'u',
    'f',
    'h',
    'c',
    'č',
    'dž',
    'š',
    'Nj',
    'Lj',
    'A',
    'B',
    'V',
    'G',
    'D',
    'Đ',
    'E',
    'Ž',
    'Z',
    'I',
    'J',
    'K',
    'L',
    'M',
    'N',
    'O',
    'P',
    'R',
    'S',
    'T',
    'Ć',
    'U',
    'F',
    'H',
    'C',
    'Č',
    'Dž',
    'Š',
  ];
  $result = str_replace($search, $replace, $string);
  return $result;
}

function cir2lat_path($string) {
  $search = [
    'њ',
    'љ',
    'а',
    'б',
    'в',
    'г',
    'д',
    'ђ',
    'e',
    'ж',
    'з',
    'и',
    'ј',
    'к',
    'л',
    'м',
    'н',
    'о',
    'п',
    'р',
    'с',
    'т',
    'ћ',
    'у',
    'ф',
    'х',
    'ц',
    'ч',
    'џ',
    'ш',
    'Њ',
    'Љ',
    'А',
    'Б',
    'В',
    'Г',
    'Д',
    'Ђ',
    'Е',
    'Ж',
    'З',
    'И',
    'Ј',
    'К',
    'Л',
    'М',
    'Н',
    'О',
    'П',
    'Р',
    'С',
    'Т',
    'Ћ',
    'У',
    'Ф',
    'Х',
    'Ц',
    'Ч',
    'Џ',
    'Ш',
  ];
  $replace = [
    'nj',
    'lj',
    'a',
    'b',
    'v',
    'g',
    'd',
    'dj',
    'e',
    'z',
    'z',
    'i',
    'j',
    'k',
    'l',
    'm',
    'n',
    'o',
    'p',
    'r',
    's',
    't',
    'c',
    'u',
    'f',
    'h',
    'c',
    'c',
    'dz',
    's',
    'nj',
    'lj',
    'a',
    'b',
    'v',
    'g',
    'd',
    'dj',
    'e',
    'z',
    'z',
    'i',
    'j',
    'k',
    'l',
    'm',
    'n',
    'o',
    'p',
    'r',
    's',
    't',
    'c',
    'u',
    'f',
    'h',
    'c',
    'c',
    'dz',
    's',
  ];
  $result = str_replace($search, $replace, $string);
  return $result;
}

function _cyr2lat_latin_path($string) {
  $search = ['Š', 'Đ', 'Č', 'Ć', 'Ž', 'š', 'đ', 'č', 'ć', 'ž'];
  $replace = ['S', 'Dj', 'C', 'C', 'Z', 's', 'dj', 'c', 'c', 'z'];
  $result = str_replace($search, $replace, $string);
  return $result;
}

// From core/modules/contextual/contextual.module.
function cyr2lat_page_attachments(array &$page) {
  $page['#attached']['library'][] = 'cyr2lat/cyr2lat-js';
}
+4 −4
Changes for cyr2lat.routing.yml: 4 added lines, 4 removed lines.
Original line number Diff line number Diff line
cyr2lat.config:
  path: '/admin/structure/cyr2lat/settings'
  path: '/admin/config/regional/cyrillic-to-latin'
  defaults:
    _form: '\Drupal\cyr2lat\Form\cyr2LatConfigForm'
    _title: 'Cyr2Lat Settings'
    _form: '\Drupal\cyr2lat\Form\Cyr2LatConfigForm'
    _title: 'Cyrillic to Latin Settings'
  requirements:
    _permission: 'administer site configuration'
 No newline at end of file
    _permission: 'administer languages'
 No newline at end of file
Loading