Commit 4c39f370 authored by Kristof De Jaeger's avatar Kristof De Jaeger
Browse files

Issue #3191374 by swentel: Allow to manage sources from reader

parent 5fe250c1
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -128,6 +128,13 @@ function indieweb_microsub_reader_channels() {
  return \Drupal::service('indieweb.microsub.reader')->getChannels();
}

/**
 * Implements hook_reader_sources().
 */
function indieweb_microsub_reader_sources($op) {
  return \Drupal::service('indieweb.microsub.reader')->getSourcesPage($op);
}

/**
 * Implements hook_reader_timeline().
 */
+6 −0
Original line number Diff line number Diff line
@@ -64,6 +64,12 @@ class MicrosubChannelForm extends ContentEntityForm {
      '#default_value' => $channel->getStatus(),
    ];

    $form['weight'] = [
      '#type' => 'weight',
      '#title' => $this->t('Weight'),
      '#default_value' => $channel->getWeight(),
    ];

    return $form;
  }

+112 −2
Original line number Diff line number Diff line
@@ -2,7 +2,10 @@

namespace Drupal\indieweb_microsub\Reader;

use Drupal\Component\Utility\Unicode;
use Drupal\Core\Link;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
use Drupal\indieweb_microsub\Entity\MicrosubChannelInterface;
use Drupal\reader\ReaderBase;

@@ -20,7 +23,7 @@ class Reader extends ReaderBase {
        ->getStorage('indieweb_microsub_channel')
        ->getQuery()
        ->condition('status', 1)
        ->sort('weight', 'ASC')
        ->sort('weight')
        ->execute();

      $channels = [];
@@ -36,7 +39,7 @@ class Reader extends ReaderBase {
      foreach (\Drupal::entityTypeManager()->getStorage('indieweb_microsub_channel')->loadMultiple($ids) as $channel) {
        $unread = 0;

        $indicator = $indicator = $channel->getReadIndicator();
        $indicator = $channel->getReadIndicator();
        if ($indicator == MicrosubChannelInterface::readIndicatorCount) {
          $unread = (int) $channel->getUnreadCount();
        }
@@ -58,6 +61,113 @@ class Reader extends ReaderBase {
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getSourcesPage($op) {

    if (!($this->microsubIsInternal() && \Drupal::currentUser()->hasPermission('access reader'))) {
      return ['#markup' => '<div class="general-content">' . $this->t('You can not managed IndieWeb sources.') . '</div>'];
    }

    switch ($op) {
      case 'add-source':
        $build = $this->addSourcesButton($this->t('Back to sources'), 'indieweb-microsub');
        $source = \Drupal::entityTypeManager()->getStorage('indieweb_microsub_source')->create();
        $form = \Drupal::service('entity.form_builder')->getForm($source, 'add');
        $form['#action'] .= '?destination=' . $this->getSourcesListUrl('indieweb-microsub');
        $build['add'] = $form;
        break;

      case 'add-channel':
        $build = $this->addSourcesButton($this->t('Back to sources'), 'indieweb-microsub');
        $source = \Drupal::entityTypeManager()->getStorage('indieweb_microsub_channel')->create();
        $form = \Drupal::service('entity.form_builder')->getForm($source, 'add');
        $form['#action'] .= '?destination=' . $this->getSourcesListUrl('indieweb-microsub');
        $build['add'] = $form;
        break;

      case 'edit':
        $type = $_GET['type'] ?? 'indieweb_microsub_source';
        $build = $this->addSourcesButton($this->t('Back to sources'), 'indieweb-microsub');
        $source = \Drupal::entityTypeManager()->getStorage($type)->load($_GET['id']);
        $form = \Drupal::service('entity.form_builder')->getForm($source, 'edit');
        $form['#action'] .= '&destination=' . $this->getSourcesListUrl('indieweb-microsub');
        $form['actions']['delete']['#access'] = FALSE;
        $build['edit'] = $form;
        break;

      case 'delete':
        $type = $_GET['type'] ?? 'indieweb_microsub_source';
        $build = $this->addSourcesButton($this->t('Back to sources'), 'indieweb-microsub');
        $source = \Drupal::entityTypeManager()->getStorage($type)->load($_GET['id']);
        $form = \Drupal::service('entity.form_builder')->getForm($source, 'delete');
        $form['actions']['cancel']['#access'] = FALSE;
        $form['#action'] .= '&destination=' . $this->getSourcesListUrl('indieweb-microsub');
        $build['delete'] = $form;
        $build['delete']['#prefix'] = $this->addSourcesConfirmDeleteText($source->label());
        break;

      default:
        $add_source = $this->addSourcesButton($this->t('+ Add source'), 'indieweb-microsub', 'add-source');
        $add_channel = $this->addSourcesButton($this->t('+ Add channel'), 'indieweb-microsub', 'add-channel');
        $build['add_source'] = $add_source;
        $build['add_channel'] = $add_channel;

        $rows = [];
        /** @var \Drupal\indieweb_microsub\Entity\MicrosubSourceInterface[] $sources */
        $channel_ids = \Drupal::entityTypeManager()->getStorage('indieweb_microsub_channel')->getQuery()
          ->sort('weight', 'ASC')->execute();
        $channels = \Drupal::entityTypeManager()->getStorage('indieweb_microsub_channel')->loadMultiple($channel_ids);
        foreach ($channels as $channel) {
          $row = [];
          $row[] = $this->t('Channel: @name', ['@name' => $channel->label()]);
          $row[] = Link::fromTextAndUrl($this->t('Edit'), Url::fromRoute('reader.sources', [
            'module' => 'indieweb-microsub',
            'op' => 'edit',
          ], ['query' => ['id' => $channel->id(), 'type' => 'indieweb_microsub_channel']]))->toString();
          $row[] = Link::fromTextAndUrl($this->t('Delete'), Url::fromRoute('reader.sources', [
            'module' => 'indieweb-microsub',
            'op' => 'delete',
          ], ['query' => ['id' => $channel->id(), 'type' => 'indieweb_microsub_channel']]))->toString();
          $rows[] = $row;

          $sources = \Drupal::entityTypeManager()->getStorage('indieweb_microsub_source')->loadByProperties(['channel_id' => $channel->id()]);
          foreach ($sources as $source) {
            $row = [];
            if (!empty($source->getName())) {
              $label = $source->getName();
            }
            else {
              $label = Unicode::truncate($source->label(), 40, FALSE, TRUE);
            }
            $row[] = $label;
            $row[] = Link::fromTextAndUrl($this->t('Edit'), Url::fromRoute('reader.sources', [
              'module' => 'indieweb-microsub',
              'op' => 'edit',
            ], ['query' => ['id' => $source->id()]]))->toString();
            $row[] = Link::fromTextAndUrl($this->t('Delete'), Url::fromRoute('reader.sources', [
              'module' => 'indieweb-microsub',
              'op' => 'delete',
            ], ['query' => ['id' => $source->id()]]))->toString();
            $rows[] = $row;
          }
        }

        $build['table'] = [
          '#type' => 'table',
          '#rows' => $rows,
          '#empty' => $this->t('No sources available'),
          '#attributes' => ['class' => ['reader-content-table']],
        ];

        break;
    }

    $build['#title'] = $this->t('Manage IndieWeb');
    return $build;
  }

  /**
   * {@inheritdoc}
   */