You are here

public function DefaultController::ccl_admin in Custom Contextual Links 8

1 string reference to 'DefaultController::ccl_admin'
ccl.routing.yml in ./ccl.routing.yml
ccl.routing.yml

File

src/Controller/DefaultController.php, line 15
Contains \Drupal\ccl\Controller\DefaultController.

Class

DefaultController
Default controller for the ccl module.

Namespace

Drupal\ccl\Controller

Code

public function ccl_admin() {
  $result = db_query("SELECT * FROM {ccl}");
  if ($result
    ->rowCount()) {

    // Get content type names.
    $cts = node_type_get_names();

    // Prepare table header.
    $header = [
      t('Title'),
      t('URL'),
      t('Type'),
      t('Options'),
      t('Operation'),
    ];
    $rows = [];
    foreach ($result as $record) {
      $options = [
        'desc' => '',
        'op' => '',
      ];

      // Prepeare the options display.
      if ($record->type == 'node') {
        $link_options = unserialize($record->options);
        switch ($link_options['node_options']) {
          case 'global':
            $options['desc'] = t('Attached to all nodes.');
            break;
          case 'ct':
            $options['desc'] = t('Attached to all nodes of the content type %ct.', [
              '%ct' => $cts[$link_options['node_type']],
            ]);
            break;
          case 'node':
            $node_title = db_query('SELECT title FROM {node} WHERE nid = :nid', [
              ':nid' => $link_options['node_id'],
            ])
              ->fetchField();
            $options['desc'] = t('Attached to %node_title [NID: !nid].', [
              '%node_title' => $node_title,
              '!nid' => $link_options['node_id'],
            ]);
            break;
        }

        // @FIXME
        // l() expects a Url object, created from a route name or external URI.
        // $options['op'] = l(t('Edit'), 'admin/config/user-interface/ccl/' . $record->clid . '/edit') . ' | ' . l(t('Delete'), 'admin/config/user-interface/ccl/' . $record->clid . '/delete');
      }
      else {
        foreach (\Drupal::moduleHandler()
          ->getImplementations('ccl_link_info') as $module) {
          $options = \Drupal::moduleHandler()
            ->invoke($module, 'ccl_link_info', [
            $record,
          ]);
          if (!empty($options)) {
            break;
          }
        }
      }

      // Prepare table row.
      $rows[] = [
        $record->title . ' <small>[ID: ' . $record->clid . ']</small>',
        $record->link,
        $record->type,
        $options['desc'],
        $options['op'],
      ];
    }

    // @FIXME
    // theme() has been renamed to _theme() and should NEVER be called directly.
    // Calling _theme() directly can alter the expected output and potentially
    // introduce security issues (see https://www.drupal.org/node/2195739). You
    // should use renderable arrays instead.
    //
    //
    // @see https://www.drupal.org/node/2195739
    // return theme('table', array('header' => $header, 'rows' => $rows));
  }
  else {

    // @FIXME
    // url() expects a route name or an external URI.
    // return array(
    //       'empty_text' => array(
    //         '#type' => 'markup',
    //         '#markup' => '<p>' . t('No custom contextual links have been added yet. <a href="@add-page">Add a link here</a>.', array('@add-page' => url('admin/config/user-interface/ccl/add'))) . '</p>',
    //       ),
    //     );
  }
}