| Conditions | 22 |
| Paths | 122 |
| Total Lines | 118 |
| Code Lines | 58 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 37 | public function addData(array $result): array |
||
| 38 | { |
||
| 39 | $table = $result['tableName']; |
||
| 40 | |||
| 41 | foreach ($result['processedTca']['columns'] as $fieldName => $fieldConfig) { |
||
| 42 | if (!isset($fieldConfig['config']['type']) || $fieldConfig['config']['type'] !== 'language') { |
||
| 43 | continue; |
||
| 44 | } |
||
| 45 | |||
| 46 | // Save user defined items and reset the field config items array afterwards |
||
| 47 | $userDefinedItems = $this->sanitizeItemArray($fieldConfig['config']['items'] ?? [], $table, $fieldName); |
||
| 48 | $fieldConfig['config']['items'] = []; |
||
| 49 | |||
| 50 | // Initialize site languages to be fetched |
||
| 51 | $siteLanguages = []; |
||
| 52 | |||
| 53 | if (($result['effectivePid'] ?? 0) === 0 || !($result['site'] ?? null) instanceof Site) { |
||
| 54 | // In case we deal with a pid=0 record or a record on a page outside |
||
| 55 | // of a site config, all languages from all sites should be added. |
||
| 56 | $sites = $this->getAllSites(); |
||
| 57 | foreach ($sites as $site) { |
||
| 58 | // Add ALL languages from ALL sites |
||
| 59 | foreach ($site->getAllLanguages() as $languageId => $language) { |
||
| 60 | if (isset($siteLanguages[$languageId])) { |
||
| 61 | // Language already provided by another site, just add the label separately |
||
| 62 | $siteLanguages[$languageId]['title'] .= ', ' . $language->getTitle() . ' [Site: ' . $site->getIdentifier() . ']'; |
||
| 63 | } else { |
||
| 64 | $siteLanguages[$languageId] = [ |
||
| 65 | 'title' => $language->getTitle() . ' [Site: ' . $site->getIdentifier() . ']', |
||
| 66 | 'flagIconIdentifier' => $language->getFlagIdentifier() |
||
| 67 | ]; |
||
| 68 | } |
||
| 69 | } |
||
| 70 | } |
||
| 71 | ksort($siteLanguages); |
||
| 72 | } elseif (($result['systemLanguageRows'] ?? []) !== []) { |
||
| 73 | // Add system languages available for the current site |
||
| 74 | foreach ($result['systemLanguageRows'] as $languageId => $language) { |
||
| 75 | if ($languageId !== -1) { |
||
| 76 | $siteLanguages[$languageId] = [ |
||
| 77 | 'title' => $language['title'], |
||
| 78 | 'flagIconIdentifier' => $language['flagIconIdentifier'], |
||
| 79 | ]; |
||
| 80 | } |
||
| 81 | } |
||
| 82 | } |
||
| 83 | |||
| 84 | if ($siteLanguages !== []) { |
||
| 85 | // In case siteLanguages are available, add the "site languages" group |
||
| 86 | $fieldConfig['config']['items'] = [ |
||
| 87 | [ |
||
| 88 | 0 => 'LLL:EXT:core/Resources/Private/Language/locallang_general.xlf:LGL.siteLanguages', |
||
| 89 | 1 => '--div--' |
||
| 90 | ] |
||
| 91 | ]; |
||
| 92 | // Add the fetched site languages to the field config items array |
||
| 93 | foreach ($siteLanguages as $languageId => $language) { |
||
| 94 | $fieldConfig['config']['items'][] = [ |
||
| 95 | 0 => $language['title'], |
||
| 96 | 1 => $languageId, |
||
| 97 | 2 => $language['flagIconIdentifier'], |
||
| 98 | ]; |
||
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 102 | // Add the "special" group for "ALL" and / or user defined items |
||
| 103 | if (($table !== 'pages' && isset($result['systemLanguageRows'][-1])) || $userDefinedItems !== []) { |
||
| 104 | $fieldConfig['config']['items'][] = [ |
||
| 105 | 0 => 'LLL:EXT:core/Resources/Private/Language/locallang_general.xlf:LGL.specialLanguages', |
||
| 106 | 1 => '--div--' |
||
| 107 | ]; |
||
| 108 | } |
||
| 109 | // Add "-1" for all TCA records except pages in case the user is allowed to. |
||
| 110 | // The item is added to the "special" group, in order to not provide it as default by accident. |
||
| 111 | if ($table !== 'pages' && isset($result['systemLanguageRows'][-1])) { |
||
| 112 | $fieldConfig['config']['items'][] = [ |
||
| 113 | 0 => 'LLL:EXT:core/Resources/Private/Language/locallang_general.xlf:LGL.allLanguages', |
||
| 114 | 1 => -1, |
||
| 115 | 2 => 'flags-multiple' |
||
| 116 | ]; |
||
| 117 | } |
||
| 118 | |||
| 119 | // Add user defined items again so they are in the "special" group |
||
| 120 | $fieldConfig['config']['items'] = array_merge($fieldConfig['config']['items'], $userDefinedItems); |
||
| 121 | |||
| 122 | // Respect TSconfig options |
||
| 123 | $fieldConfig['config']['items'] = $this->removeItemsByKeepItemsPageTsConfig($result, $fieldName, $fieldConfig['config']['items']); |
||
| 124 | $fieldConfig['config']['items'] = $this->addItemsFromPageTsConfig($result, $fieldName, $fieldConfig['config']['items']); |
||
| 125 | $fieldConfig['config']['items'] = $this->removeItemsByRemoveItemsPageTsConfig($result, $fieldName, $fieldConfig['config']['items']); |
||
| 126 | |||
| 127 | // In case no items are set at this point, we can write this back and continue with the next column |
||
| 128 | if ($fieldConfig['config']['items'] === []) { |
||
| 129 | $result['processedTca']['columns'][$fieldName] = $fieldConfig; |
||
| 130 | continue; |
||
| 131 | } |
||
| 132 | |||
| 133 | // Check current database value |
||
| 134 | $currentDatabaseValue = (int)($result['databaseRow'][$fieldName] ?? 0); |
||
| 135 | if (!in_array($currentDatabaseValue, array_map('intval', array_column($fieldConfig['config']['items'], 1)), true)) { |
||
| 136 | // Current value is invalid, so add it with a proper message at the top |
||
| 137 | $fieldConfig['config']['items'] = $this->addInvalidItem($result, $table, $fieldName, $currentDatabaseValue, $fieldConfig['config']['items']); |
||
| 138 | } |
||
| 139 | |||
| 140 | // Reinitialize array keys |
||
| 141 | $fieldConfig['config']['items'] = array_values($fieldConfig['config']['items']); |
||
| 142 | |||
| 143 | // In case the last element is a divider, remove it |
||
| 144 | if ((string)($fieldConfig['config']['items'][array_key_last($fieldConfig['config']['items'])][1] ?? '') === '--div--') { |
||
| 145 | array_pop($fieldConfig['config']['items']); |
||
| 146 | } |
||
| 147 | |||
| 148 | // Translate labels |
||
| 149 | $fieldConfig['config']['items'] = $this->translateLabels($result, $fieldConfig['config']['items'], $table, $fieldName); |
||
| 150 | |||
| 151 | $result['processedTca']['columns'][$fieldName] = $fieldConfig; |
||
| 152 | } |
||
| 153 | |||
| 154 | return $result; |
||
| 155 | } |
||
| 186 |