You are here

public function RedhenConnections::list in RedHen CRM 8

Displays add links for available bundles/types for redhen_connection.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The current request object.

Return value

array A table of connections that are made to this content.

File

modules/redhen_connection/src/Controller/RedhenConnections.php, line 51

Class

RedhenConnections
Class RedhenConnections.

Namespace

Drupal\redhen_connection\Controller

Code

public function list(Request $request) {

  /** @var ConnectionServiceInterface $connection_service */
  $connection_service = \Drupal::service('redhen_connection.connections');
  $entity = redhen_connection_get_connection_entity_from_route();
  $entity_type_key = $entity
    ->getEntityTypeId();
  $connections = $connection_service
    ->getConnections($entity);

  // Creates the table header.
  $header = [
    'Type',
    'Name',
    'Operations',
  ];
  $add_url = Url::fromRoute("{$entity_type_key}.connection.add_page", [
    $entity_type_key => $entity
      ->id(),
  ], [
    'absolute' => TRUE,
  ])
    ->toString();
  $rows = [];
  foreach ($connections as $connection) {
    $view = Link::createFromRoute($connection
      ->label()
      ->render(), 'entity.redhen_connection.canonical', [
      'redhen_connection' => $connection
        ->id(),
    ])
      ->toString();
    $edit = Link::createFromRoute('Edit', 'entity.redhen_connection.edit_form', [
      'redhen_connection' => $connection
        ->id(),
    ])
      ->toString();
    $delete = Link::createFromRoute('Delete', 'entity.redhen_connection.delete_form', [
      'redhen_connection' => $connection
        ->id(),
    ])
      ->toString();
    $row = [
      'data' => [
        $connection
          ->getType(),
        new FormattableMarkup("@view", [
          '@view' => $view,
        ]),
        new FormattableMarkup("@edit @delete", [
          '@edit' => $edit,
          '@delete' => $delete,
        ]),
      ],
    ];
    $rows[] = $row;
  }

  // Build the table.
  // @todo add actual link for adding a connection to this the given entity.
  $build = [
    'table' => [
      '#theme' => 'table',
      '#prefix' => "<ul class='action-links'><li><a class='button button-action button--primary button--small' href=" . $add_url . ">Add Connection</a></li></ul>",
      '#attributes' => [
        'data-striping' => 0,
      ],
      '#header' => $header,
      '#rows' => $rows,
    ],
  ];
  return $build;
}