public function DropdownLanguage::build in Dropdown Language 8.3
Same name and namespace in other branches
- 8 src/Plugin/Block/DropdownLanguage.php \Drupal\dropdown_language\Plugin\Block\DropdownLanguage::build()
- 8.2 src/Plugin/Block/DropdownLanguage.php \Drupal\dropdown_language\Plugin\Block\DropdownLanguage::build()
- 3.0.x src/Plugin/Block/DropdownLanguage.php \Drupal\dropdown_language\Plugin\Block\DropdownLanguage::build()
Builds and returns the renderable array for this block plugin.
If a block should not be rendered because it has no content, then this method must also ensure to return no content: it must then only return an empty array, or an empty array with #cache set (with cacheability metadata indicating the circumstances for it being empty).
Return value
array A renderable array representing the content of the block.
Overrides BlockPluginInterface::build
See also
\Drupal\block\BlockViewBuilder
File
- src/
Plugin/ Block/ DropdownLanguage.php, line 120
Class
- DropdownLanguage
- Provides an alternative language switcher block.
Namespace
Drupal\dropdown_language\Plugin\BlockCode
public function build() {
$block = [];
$build = [];
$languages = $this->languageManager
->getLanguages();
if (count($languages) > 1) {
$derivative_id = $this
->getDerivativeId();
$route = $this->pathMatcher
->isFrontPage() ? '<front>' : '<current>';
$current_language = $this->languageManager
->getCurrentLanguage($derivative_id)
->getId();
$links = $this->languageManager
->getLanguageSwitchLinks($derivative_id, Url::fromRoute($route))->links;
// Place active language ontop of list.
if (isset($links[$current_language])) {
$links = [
$current_language => $links[$current_language],
] + $links;
// Set an active class for styling.
$links[$current_language]['attributes']['class'][] = 'active-language';
// Remove self-referencing link.
$links[$current_language]['url'] = Url::fromRoute('<nolink>');
}
// Get block instance and general settings.
$block_config = $this
->getConfiguration();
$general_config = $this->configFactory
->get('dropdown_language.setting');
$wrapper_default = $general_config
->get('wrapper');
$display_language_id = $general_config
->get('display_language_id');
$filter_untranslated = $general_config
->get('filter_untranslated');
$always_show_block = $general_config
->get('always_show_block');
// Only load once, rather than in switch (in a loop).
$native_names = FALSE;
if ($display_language_id == 2) {
$native_names = $this->languageManager
->getStandardLanguageList();
}
/**
* Discover the entity we are currently viewing.
* Note: page manager (and other) entities need routines. @v3 plugin.
*
* With mulitle param:
* a. items may be of any type.
* b. last object seems be ideal target object to use as 'self'.
*
**/
$entity = FALSE;
if ($filter_untranslated == '1') {
$routedItems = $this->routeMatch;
foreach ($routedItems
->getParameters() as $param) {
if ($param instanceof EntityInterface) {
$entity['EntityInterface'] = $param;
}
}
}
foreach ($links as $lid => $link) {
// Re-label as per general setting.
switch ($display_language_id) {
case '1':
$links[$lid]['title'] = mb_strtoupper($lid);
break;
case '2':
$name = $link['language']
->getName();
$links[$lid]['title'] = isset($native_names[$lid]) ? $native_names[$lid][1] : $name;
if (isset($native_names[$lid]) && (isset($native_names[$lid]) && $native_names[$lid][1] != $name)) {
$links[$lid]['attributes']['title'] = $name;
}
break;
case '3':
$links[$lid]['title'] = isset($block_config['labels'][$lid]) ? $block_config['labels'][$lid] : $link['language']
->getName();
break;
}
// Removes unused languages from the dropdown. @v3
if ($entity && $entity['EntityInterface'] && $filter_untranslated == '1') {
$has_translation = method_exists($entity['EntityInterface'], 'getTranslationStatus') ? $entity['EntityInterface']
->getTranslationStatus($lid) : FALSE;
$this_translation = $has_translation && method_exists($entity['EntityInterface'], 'getTranslation') ? $entity['EntityInterface']
->getTranslation($lid) : FALSE;
$access_translation = $this_translation && method_exists($this_translation, 'access') && $this_translation
->access('view') ? TRUE : FALSE;
if (!$this_translation || !$access_translation) {
unset($links[$lid]);
}
}
}
$dropdown_button = [
'#type' => 'dropbutton',
'#subtype' => 'dropdown_language',
'#links' => $links,
'#attributes' => [
'class' => [
'dropdown-language-item',
],
],
'#attached' => [
'library' => [
'dropdown_language/dropdown-language-selector',
],
],
];
if ($wrapper_default == 1) {
$block['switcher'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Switch Language'),
];
$block['switcher']['switch-language'] = $dropdown_button;
}
else {
$block['switch-language'] = $dropdown_button;
}
}
if (count($links) > 1 || $always_show_block) {
$build['dropdown-language'] = $block;
}
return $build;
}