Drupal - Получение идентификатора узла от представления настроить ссылку в блоке

Существует большое "неопределенное поведение". Можно изучить, как избежать их читающий хорошие книги и читающий стандарты.

6
задан Peter Mortensen 1 September 2010 в 11:00
поделиться

4 ответа

Here is a more-robust way of getting the node ID:

<?php
    // Check that the current URL is for a specific node:
    if(arg(0) == 'node' && is_numeric(arg(1))) {
        return arg(1); // Return the NID
    }
    else { // Whatever it is we're looking at, it's not a node
      return NULL; // Return an invalid NID
    }
?>

This method works even if you have a custom path for your node with the path and/or pathauto modules.

Just for reference, if you don't turn on the path module, the default URLs that Drupal generates are called "system paths" in the documentation. If you do turn on the path module, you are able to set custom paths which are called "aliases" in the documentation.

Since I always have the path module turned on, one thing that confused me at first was whether it was ever possible for the arg function to return part of an alias rather than part of system path.

As it turns out, the arg function will always return a system path because the arg function is based on $_GET['q']... After a bit of research it seems that $_GET['q'] will always return a system path.

If you want to get the path from the actual page request, you need to use $_REQUEST['q']. If the path module is enabled, $_REQUEST['q'] may return either an alias or a system path.

14
ответ дан 8 December 2019 в 04:30
поделиться

You should considder the panels module. It is a very big module and requires some work before you really can tap into it's potential. So take that into considderation.

You can use it to setup a page containing several views/blocks that can be placed in different regions. It uses a concept called context which can be anything related to what you are viewing. You can use that context to determine which node is being viewed and not only change blocks but also layout. It is also a bit more clean since you can move the PHP code away from admin interface.

On a side note, it's also written by the views author.

1
ответ дан 8 December 2019 в 04:30
поделиться

Есть несколько способов сделать это:

  1. Вы можете создавать блоки с помощью Views и передавать nid через аргумент.

  2. Вы можете вручную передать nid путем доступа к объекту $ view , используя приведенный ниже код. Это массив по адресу $ view-> result . Каждая строка в представлении является объектом в этом массиве, и nid находится в этом объекте для каждого из них. Таким образом, вы можете запустить для этого foreach и довольно легко получить все nid всех строк в представлении.

Первый вариант намного проще, поэтому, если это соответствует вашим потребностям, я бы выбрал его.

1
ответ дан 8 December 2019 в 04:30
поделиться

В конце концов этот фрагмент выполнил свою работу - он просто удалил чистый URL и вернул самый последний аргумент.

<?php
    $refer= $_SERVER ['REQUEST_URI'];
    $nid = explode("/", $refer);
    $nid = $nid[3];
?>

Учитывая ответ на комментарий, приведенное выше было вероятно, сводится к этому, используя функцию Drupal arg () для получения части пути запроса:

<?php
    $nid = arg(3);
?>
2
ответ дан 8 December 2019 в 04:30
поделиться
Другие вопросы по тегам:

Похожие вопросы: