Skip to content

Commit

Permalink
Merge pull request #13 from donquixote/group-integration-links
Browse files Browse the repository at this point in the history
Add view field links.
  • Loading branch information
donquixote authored and AaronGilMartinez committed Nov 22, 2024
2 parents 919ddd1 + 4dc6554 commit 7065314
Show file tree
Hide file tree
Showing 10 changed files with 751 additions and 37 deletions.
23 changes: 23 additions & 0 deletions collabora_online.views.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

/**
* Implements hook_views_data_alter().
*/
function collabora_online_views_data_alter(array &$data): void {
$data['media']['collabora_preview'] = [
'title' => t('Link to view in Collabora Online'),
'group' => t('Media'),
'field' => [
'id' => 'media_collabora_preview',
],
];
$data['media']['collabora_edit'] = [
'title' => t('Link to edit in Collabora Online'),
'group' => t('Media'),
'field' => [
'id' => 'media_collabora_edit',
],
];
}
12 changes: 12 additions & 0 deletions config/schema/collabora_online.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,15 @@ collabora_online.settings:
allowfullscreen:
type: boolean
label: 'Allow full-screen.'

views.field.media_collabora_preview:
type: views_field
label: 'Collabora view link'
mapping:
text:
type: label
label: 'Text to display'

views.field.media_collabora_edit:
type: views.field.media_collabora_preview
label: 'Collabora edit link'
56 changes: 56 additions & 0 deletions modules/collabora_online_group/collabora_online_group.install
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

use Drupal\views\Entity\View;

/**
* Implements hook_install().
*/
function collabora_online_group_install(bool $is_syncing): void {
if (
$is_syncing ||
!\Drupal::moduleHandler()->moduleExists('views') ||
!($view = View::load('group_media'))
) {
return;
}

// Load display and apply changes.
$display = &$view->getDisplay('default');
if ($display === NULL) {
return;
}

// Add new fields to the display.
$display['display_options']['fields'] += [
'collabora_preview' => [
'id' => 'collabora_preview',
'table' => 'media',
'field' => 'collabora_preview',
'plugin_id' => 'media_collabora_preview',
'label' => '',
'exclude' => TRUE,
'text' => t('View in Collabora Online'),
],
];
$display['display_options']['fields'] += [
'collabora_edit' => [
'id' => 'collabora_edit',
'table' => 'media',
'field' => 'collabora_edit',
'plugin_id' => 'media_collabora_edit',
'label' => '',
'exclude' => TRUE,
'text' => t('Edit in Collabora Online'),
],
];
// Add new fields as options for the dropbutton and move the element to the end.
$dropbutton = $display['display_options']['fields']['dropbutton'];
$dropbutton['fields'] += [
'collabora_preview' => 'collabora_preview',
'collabora_edit' => 'collabora_edit',
];
unset($display['display_options']['fields']['dropbutton']);
$display['display_options']['fields']['dropbutton'] = $dropbutton;

$view->save();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<?php

declare(strict_types=1);

namespace Drupal\Tests\collabora_online_group\Functional;

use Drupal\Core\Url;
use Drupal\group\PermissionScopeInterface;
use Drupal\media\Entity\Media;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\collabora_online\Traits\MediaCreationTrait;
use Drupal\Tests\collabora_online_group\Traits\GroupRelationTrait;
use Drupal\Tests\group\Traits\GroupTestTrait;
use Drupal\Tests\media\Traits\MediaTypeCreationTrait;
use Drupal\user\RoleInterface;

/**
* Test the modifications performend in groupmedia view by the module.
*/
class GroupMediaViewsTest extends BrowserTestBase {

use MediaCreationTrait;
use GroupRelationTrait;
use GroupTestTrait;
use MediaTypeCreationTrait;

/**
* {@inheritdoc}
*/
protected static $modules = [
'collabora_online_group',
'views',
];

/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';

/**
* Test the links for Collabora operations in the view.
*/
public function testViewLinks(): void {
// Add configuration needed for testing.
$group_type = $this->createGroupType(['id' => 'group_type_1']);
$this->createMediaType('file', [
'id' => 'document',
'label' => 'Document',
]);
$this->createGroupRole([
'group_type' => 'group_type_1',
'scope' => PermissionScopeInterface::INSIDER_ID,
'global_role' => RoleInterface::AUTHENTICATED_ID,
'permissions' => [
'view group',
'access group_media overview',
'view group_media:document entity',
'edit any group_media:document in collabora',
'preview group_media:document in collabora',
],
]);
$this->createPluginRelation($group_type, 'group_media:document', [
'group_cardinality' => 0,
'entity_cardinality' => 1,
'use_creation_wizard' => FALSE,
]);

// Create content.
$group = $this->createGroup(['type' => 'group_type_1']);
for ($i = 0; $i < 3; $i++) {
$media = $this->createMediaEntity('document', [
'id' => 'media_' . $i,
'name' => 'Media ' . $i,
]);
$group->addRelationship($media, 'group_media:document');
}
$user = $this->createUser([
'view the administration theme',
'access administration pages',
'access group overview',
]);
$group->addMember($user);

// Go to the page and check the links added to the view.
$this->drupalLogin($user);
$this->drupalGet("group/{$group->id()}/media");
$assert_session = $this->assertSession();

// Check table header.
$table = $assert_session->elementExists('css', 'table');
$table_header = $assert_session->elementExists('css', 'thead', $table);
$rows = $table_header->findAll('css', 'tr');
$cols = $rows[0]->findAll('css', 'th');
$this->assertEquals('Media name', $cols[0]->getText());
$this->assertEquals('Bundle', $cols[1]->getText());
$this->assertEquals('Status', $cols[2]->getText());
$this->assertEquals('Publisher', $cols[3]->getText());
// Support different versions of groupmedia.
$this->assertTrue(in_array($cols[4]->getText(), ['Operations', 'Dropbutton']));

// Check that rows contain new links for operations in Collabora.
$table_body = $assert_session->elementExists('css', 'tbody', $table);
$rows = $table_body->findAll('css', 'tr');
$i = 0;
foreach (Media::loadMultiple() as $media) {
$cols = $rows[$i]->findAll('css', 'td');
$this->assertEquals('Media ' . $i, $cols[0]->getText());
$this->assertEquals('Document', $cols[1]->getText());
$this->assertEquals('Yes', $cols[2]->getText());
$this->assertEquals('Anonymous', $cols[3]->getText());
$operation_links = $cols[4]->findAll('css', 'a');
$this->assertEquals('View in Collabora Online', $operation_links[0]->getText());
$this->assertEquals(
Url::fromRoute(
'collabora-online.view',
[
'media' => $media->id()
],
[
'query' => [
'destination' => "/group/{$group->id()}/media",
]
]
)->toString(),
$operation_links[0]->getAttribute('href')
);
$this->assertEquals('Edit in Collabora Online', $operation_links[1]->getText());
$this->assertEquals(
Url::fromRoute(
'collabora-online.edit',
[
'media' => $media->id()
],
[
'query' => [
'destination' => "/group/{$group->id()}/media",
]
]
)->toString(),
$operation_links[1]->getAttribute('href')
);
$i++;
}
}

}
36 changes: 0 additions & 36 deletions modules/collabora_online_group/tests/src/Functional/SmokeTest.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected function createPluginRelation(GroupTypeInterface $group_type, string $
$entity_type_id = 'group_relationship_type';

// Fallback for older versions.
if ($this->entityTypeManager()->getDefinition($entity_type_id, FALSE) === NULL) {
if ($this->entityTypeManager()->getDefinition($entity_type_id, FALSE) === NULL) {
$entity_type_id = 'group_content_type';
}

Expand Down
42 changes: 42 additions & 0 deletions src/Plugin/views/field/CollaboraEdit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Drupal\collabora_online\Plugin\views\field;

use Drupal\collabora_online\Cool\CoolUtils;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;
use Drupal\views\Attribute\ViewsField;
use Drupal\views\Plugin\views\field\LinkBase;
use Drupal\views\ResultRow;

/**
* Field handler for link to edit a collabora file.
*
* @ingroup views_field_handlers
*/
#[ViewsField('media_collabora_edit')]
class CollaboraEdit extends LinkBase {

/**
* {@inheritdoc}
*/
protected function getUrlInfo(ResultRow $row): Url|null {
/** @var \Drupal\media\MediaInterface $entity */
$entity = $this->getEntity($row);

if ($entity === NULL) {
return NULL;
}

return CoolUtils::getEditorUrl($entity, TRUE);
}

/**
* {@inheritdoc}
*/
protected function getDefaultLabel(): TranslatableMarkup {
return $this->t('Edit in Collabora Online');
}
}
42 changes: 42 additions & 0 deletions src/Plugin/views/field/CollaboraPreview.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Drupal\collabora_online\Plugin\views\field;

use Drupal\collabora_online\Cool\CoolUtils;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;
use Drupal\views\Attribute\ViewsField;
use Drupal\views\Plugin\views\field\LinkBase;
use Drupal\views\ResultRow;

/**
* Field handler for link to preview a collabora file.
*
* @ingroup views_field_handlers
*/
#[ViewsField('media_collabora_preview')]
class CollaboraPreview extends LinkBase {

/**
* {@inheritdoc}
*/
protected function getUrlInfo(ResultRow $row): Url|null {
/** @var \Drupal\media\MediaInterface $entity */
$entity = $this->getEntity($row);

if ($entity === NULL) {
return NULL;
}

return CoolUtils::getEditorUrl($entity, FALSE);
}

/**
* {@inheritdoc}
*/
protected function getDefaultLabel(): TranslatableMarkup {
return $this->t('View in Collabora Online');
}
}
Loading

0 comments on commit 7065314

Please sign in to comment.