Symfony Вопрос от новичка

Olegs

Новичок
Изучаю Drupal 8 , он сделан на symfony, прошу пояснить следующий вопрос:
вот функция

Код:
use Drupal\comment\CommentInterface;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Url;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\user\Entity\User;
PHP:
function template_preprocess_forums(&$variables) {
  $variables['tid'] = $variables['term']->id();
  if ($variables['forums_defined'] = count($variables['forums']) || count($variables['parents'])) {
    if (!empty($variables['forums'])) {
      $variables['forums'] = array(
        '#theme' => 'forum_list',
        '#forums' => $variables['forums'],
        '#parents' => $variables['parents'],
        '#tid' => $variables['tid'],
      );
    }

    if ($variables['term'] && empty($variables['term']->forum_container->value) && !empty($variables['topics'])) {
      $forum_topic_list_header = $variables['header'];

      $table = array(
        '#theme' => 'table__forum_topic_list',
        '#responsive' => FALSE,
        '#attributes' => array('id' => 'forum-topic-' . $variables['tid']),
        '#header' => array(),
        '#rows' => array(),
      );

      if (!empty($forum_topic_list_header)) {
        $table['#header'] = $forum_topic_list_header;
      }

      /** @var \Drupal\node\NodeInterface $topic */
      foreach ($variables['topics'] as $id => $topic) {
        $variables['topics'][$id]->icon = array(
          '#theme' => 'forum_icon',
          '#new_posts' => $topic->new,
          '#num_posts' => $topic->comment_count,
          '#comment_mode' => $topic->comment_mode,
          '#sticky' => $topic->isSticky(),
          '#first_new' => $topic->first_new,
        );

        // We keep the actual tid in forum table, if it's different from the
        // current tid then it means the topic appears in two forums, one of
        // them is a shadow copy.
        if ($variables['tid'] != $topic->forum_tid) {
          $variables['topics'][$id]->moved = TRUE;
          $variables['topics'][$id]->title = $topic->getTitle();
          $variables['topics'][$id]->message = \Drupal::l(t('This topic has been moved'), new Url('forum.page', ['taxonomy_term' => $topic->forum_tid]));
        }
        else {
          $variables['topics'][$id]->moved = FALSE;
          $variables['topics'][$id]->title_link = \Drupal::l($topic->getTitle(), $topic->urlInfo());
          $variables['topics'][$id]->message = '';
        }
        $forum_submitted = array('#theme' => 'forum_submitted', '#topic' => (object) array(
          'uid' => $topic->getOwnerId(),
          'name' => $topic->getOwner()->getDisplayName(),
          'created' => $topic->getCreatedTime(),
        ));
        $variables['topics'][$id]->submitted = drupal_render($forum_submitted);
        $forum_submitted = array(
          '#theme' => 'forum_submitted',
          '#topic' => isset($topic->last_reply) ? $topic->last_reply : NULL,
        );
        $variables['topics'][$id]->last_reply = drupal_render($forum_submitted);

        $variables['topics'][$id]->new_text = '';
        $variables['topics'][$id]->new_url = '';

        if ($topic->new_replies) {
          $page_number = \Drupal::entityManager()->getStorage('comment')
            ->getNewCommentPageNumber($topic->comment_count, $topic->new_replies, $topic, 'comment_forum');
          $query = $page_number ? array('page' => $page_number) : NULL;
          $variables['topics'][$id]->new_text = \Drupal::translation()->formatPlural($topic->new_replies, '1 new post<span class="visually-hidden"> in topic %title</span>', '@count new posts<span class="visually-hidden"> in topic %title</span>', array('%title' => $variables['topics'][$id]->label()));
          $variables['topics'][$id]->new_url = \Drupal::url('entity.node.canonical', ['node' => $topic->id()], ['query' => $query, 'fragment' => 'new']);
        }

        // Build table rows from topics.
        $row = array();
        $row[] = array(
          'data' => array(
            $topic->icon,
            array(
              '#markup' => '<div class="forum__title"><div>' . $topic->title_link . '</div><div>' . $topic->submitted . '</div></div>',
            ),
          ),
          'class' => array('forum__topic'),
        );

        if ($topic->moved) {
          $row[] = array(
            'data' => $topic->message,
            'colspan' => '2',
          );
          
        }
        else {
          $new_replies = '';
          if ($topic->new_replies) {
            $new_replies = '<br /><a href="' . $topic->new_url . '">' . $topic->new_text . '</a>';
            
          }
$variables['mark']='833'.$topic->last_reply;
          $row[] = array(
            'data' => [
              [
                '#prefix' => $topic->comment_count,
                '#markup' => $new_replies,
              ],
            ],
            'class' => array('forum__replies'),
          );
          $row[] = array(
            'data' => $topic->last_reply,
            'class' => array('forum__last-reply'),
          );
        }
        $table['#rows'][] = $row;
      }

      $variables['topics'] = $table;
      $variables['topics_pager'] = array(
        '#type' => 'pager',
      );
    }
  }
}
В ней куча обращений к методам и свойствам класса. Вот таких $topic->comment_count.
Но объект класса $topic не создается в модуле (по крайней мере в текущем файле)
Как это работает?
 

c0dex

web.dev 2002-...
Команда форума
Партнер клуба
Потому, как он передается в цикл уже будучи объектом:

foreach ($variables['topics'] as $id => $topic) ...
 

Olegs

Новичок
тогда почему не получается повторить то же самое в другом модуле (мной созданном).
все тоже делаю, но ничего не срабатывает
PHP:
 function forum_dop_preprocess_forums(&$variables){
  
      foreach ($variables['topics'] as $id => $topic){
      
       $variables['tab']['#mark']=$topic->title_link;
    
        }
пусто
а если метод
Error: Call to a member function getCreatedTime() on string in forum_dop_preprocess_forums() (line 30 of modules/forum_dop/forum_dop.module).
и где найти класс где все эти свойства задаются?
 
Последнее редактирование:

keltanas

marty cats
тогда почему не получается повторить то же самое в другом модуле (мной созданном).
все тоже делаю, но ничего не срабатывает
пусто
а если метод
и где найти класс где все эти свойства задаются?
Тебе нужен не "symfony", тебе нужен xdebug.
 
Сверху