Попытка отобразить или отобразить значения из поля ссылок в пределах поля абзаца на его текущем языке - PullRequest
0 голосов
/ 16 января 2019

В настоящее время я использую Drupal 8 Paragraph и пытаюсь выяснить, как можно отобразить или вывести URL-адрес и заголовок поля ссылки из базы Paragraph на текущем языке с помощью предварительной обработки , Вот фрагмент кода, над которым я работаю.

function iom_preprocess_paragraph(&$variables) {

    $language = \Drupal::languageManager()->getCurrentLanguage();
    $variables['lang_code'] = $language->getId();
}

function _generatePrimaryLinkElement($currentNode, $entity, $title, $url) {

    $attributes = new Attribute(['class' => ['menu-item']]);

    if ($entity && ($currentNode->id() === $entity->id())) {
        $attributes['class'][] = '-active';
    }

    return [
        'attributes' => $attributes,
        'link' => [
            'attributes' => new Attribute([
                'href' => $url,
                'class' => ['link']
            ])
        ],
        'title' => $title
    ];
}

function _preprocess_primary_links_paragraph($paragraph, &$variables) {
    // Current page node
    $currentNode = \Drupal::request()->attributes->get('node');
    $activeLanguage = \Drupal::languageManager()->getCurrentLanguage()->getId();
    $links = [];

    foreach ($paragraph->field_link as $link) {
        $entity = _loadEntityByUri($link->uri);

        $title = $link->title;

        // If there is an entity and the title is not set of the link
        // use the entity title instead
        if ($entity && !$title) {
            $title = $entity->title->value;
        }

        $links[] = _generatePrimaryLinkElement($currentNode, $entity, $title, $link->getUrl()->toString());
    }

    $variables['label'] = $paragraph->field_label->value;
    $variables['links'] = $links;
}

<div class="sidebar-menu-group primary-link">
    <span class="label">{{ label }}</span>
    <ul class="menu -sidebar">


        {% for link in links %}

            <li {{link.attributes}}>
                <a {{link.link.attributes}}>{{link.title}}</a>
            </li>
        {% endfor %}
    </ul>
</div>
...