You are here

public function HostListBuilder::buildRow in http:BL 8

Builds a row for an entity in the entity listing.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity for this row of the list.

Return value

array A render array structure of fields for this entity.

Overrides EntityListBuilder::buildRow

See also

\Drupal\Core\Entity\EntityListBuilder::render()

File

src/Entity/Controller/HostListBuilder.php, line 89

Class

HostListBuilder
Provides a list controller for a host entity.

Namespace

Drupal\httpbl\Entity\Controller

Code

public function buildRow(EntityInterface $entity) {

  /* @var $entity \Drupal\httpbl\Entity\Host */
  $row['id'] = $entity
    ->id();

  //$row['name'] = $entity->link();

  // We don't really need to "view" the entity as content; there is nothing
  // interesting to see there. So override the default link with a link to a
  // profile on Project Honeypot.
  $host_ip = $entity->host_ip->value;
  $url = \Drupal\Core\Url::fromUri('http://www.projecthoneypot.org/search_ip.php?ip=' . $host_ip);
  $url_options = [
    'attributes' => [
      'target' => '_blank',
      'title' => 'View this host\'s profile on Project Honeypot.',
    ],
  ];
  $url
    ->setOptions($url_options);
  $project_link = \Drupal\Core\Link::fromTextAndUrl(t($host_ip), $url)
    ->toString();
  $row['name'] = $project_link;

  // Status with humanized conversion.
  $httpblManager = \Drupal::service('httpbl.evaluator');
  $human = $httpblManager
    ->getHumanStatus($entity->host_status->value);
  $row['host_status'] = t($entity->host_status->value . ' - <em style="color: lightgrey;">' . $human . '</em>');

  // If this entity is blacklisted && Ban module exists...
  if ($entity->host_status->value == HTTPBL_LIST_BLACK && \Drupal::moduleHandler()
    ->moduleExists('ban')) {

    // Also check if it has been banned.
    $ip = $entity->host_ip->value;
    $banManager = \Drupal::service('ban.ip_manager');

    // If this host is also found in ban_ip table...
    if ($banManager
      ->isBanned($ip)) {

      // Report as banned on the list, in addition to being blacklisted.
      $row['host_status'] = t($entity->host_status->value . ' - <em style="color: lightgrey;">' . $human . ' and Banned!</em>');
    }
  }

  // Source of this evaluation.
  // If this is original, un-altered (no admin management decisions) Project
  // Honeypot source, then also provide the project link here.
  if ($entity->source->value == t(HTTPBL_ORIGINAL_SOURCE)) {

    // recycle the url from above...
    $url
      ->setOptions($url_options);
    $project_link = \Drupal\Core\Link::fromTextAndUrl(t($entity->source->value), $url)
      ->toString();
    $row['source'] = $project_link;
  }
  else {
    $row['source'] = $entity->source->value;
  }

  // Expiration...
  // If expire has zero time left, show  as "next cron".
  // Otherwise, show expire timestamp formatted as "time until."
  if ($entity->expire->value < \Drupal::time()
    ->getRequestTime()) {
    $row['expire'] = t('(next cron)');
  }
  else {
    $row['expire'] = \Drupal::service('date.formatter')
      ->formatTimeDiffUntil($entity->expire->value);
  }

  // Created and Changed
  $row['created'] = \Drupal::service('date.formatter')
    ->format($entity->created->value, 'html_date');
  $row['changed'] = \Drupal::service('date.formatter')
    ->format($entity->changed->value, 'html_datetime');
  return $row + parent::buildRow($entity);
}