You are here

function ccl_admin in Custom Contextual Links 8

Same name and namespace in other branches
  1. 7 ccl.admin.inc \ccl_admin()

CCL adminitsration form.

1 string reference to 'ccl_admin'
ccl_menu in ./ccl.module
Implements hook_menu().

File

./ccl.admin.inc, line 10
Provides administrative functions for ccl.

Code

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 = array(
      t('Title'),
      t('URL'),
      t('Type'),
      t('Options'),
      t('Operation'),
    );
    $rows = array();
    foreach ($result as $record) {
      $options = array(
        '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.', array(
              '%ct' => $cts[$link_options['node_type']],
            ));
            break;
          case 'node':
            $node_title = db_query('SELECT title FROM {node} WHERE nid = :nid', array(
              ':nid' => $link_options['node_id'],
            ))
              ->fetchField();
            $options['desc'] = t('Attached to %node_title [NID: !nid].', array(
              '%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[] = array(
        $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>',
    //       ),
    //     );
  }
}