Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Commit

Permalink
Feature/provide metadata (#7)
Browse files Browse the repository at this point in the history
* map meta data to fields
  • Loading branch information
dbosen committed Jul 14, 2016
1 parent 3f745fb commit d6b246d
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 16 deletions.
4 changes: 2 additions & 2 deletions nexx_integration.routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ nexx_integration.omnia_notification_gateway:
_user_is_logged_in: 'TRUE'
_permission: 'use omnia notification gateway'
defaults:
_controller: '\Drupal\nexx_integration\Controller\Omnia::video'
_controller: '\Drupal\nexx_integration\Controller\OmniaController::video'

nexx_integration.list:
path: '/admin/content/nexx'
defaults:
_controller: '\Drupal\nexx_integration\Controller\Omnia::videoList'
_controller: '\Drupal\nexx_integration\Controller\OmniaController::videoList'
requirements:
_permission: 'edit content'
55 changes: 42 additions & 13 deletions src/Controller/Omnia.php → src/Controller/OmniaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*
* @package Drupal\nexx_integration\Controller
*/
class Omnia extends ControllerBase {
class OmniaController extends ControllerBase {
/**
* The database service.
*
Expand Down Expand Up @@ -257,14 +257,14 @@ protected function mapData(MediaInterface $media, $videoData) {
}
}

if ($actorField && !empty($actor_ids)) {
$actor_ids = array_map(array($this, 'mapTermId'), $actor_ids);
$media->$actorField = $actor_ids;
if ($actorField) {
$mapped_actor_ids = $this->mapMultipleTermIds($actor_ids);
$media->$actorField = $mapped_actor_ids;
}

if ($tagField && !empty($tag_ids)) {
$tag_ids = array_map(array($this, 'mapTermId'), $tag_ids);
$media->$tagField = $tag_ids;
if ($tagField) {
$mapped_tag_ids = $this->mapMultipleTermIds($tag_ids);
$media->$tagField = $mapped_tag_ids;
}

if ($teaserImageField && $media->$videoField->thumb !== $videoData->itemData->thumb) {
Expand All @@ -278,22 +278,51 @@ protected function mapData(MediaInterface $media, $videoData) {
}
}

/**
* Map multiple omnia term ids to drupal term ids.
*
* @param int[] $omnia_ids
* Array of omnia termn ids.
*
* @return int[] $drupal_ids
* Array of mapped drupal ids, might contain less ids then the input array.
*/
protected function mapMultipleTermIds($omnia_ids) {
$drupal_ids = [];
foreach ($omnia_ids as $omnia_id) {
$drupalId = $this->mapTermId($omnia_id);
if ($drupalId) {
$drupal_ids[] = $drupalId;
}
else {
$this->logger->warning('Unknown omnia ID @term_id"', array(
'@term_id' => $omnia_id,
)
);
}
}

return $drupal_ids;
}

/**
* Map omnia term Id to corresponding drupal term id.
*
* @param int $omniaId
* @param int $omnia_id
* The omnia id of the term.
*
* @return int $nexx_id
* @return int $drupalId
* The drupal id of the term.
*/
protected function mapTermId($omniaId) {
protected function mapTermId($omnia_id) {
$result = $this->database->select('nexx_taxonomy_term_data', 'n')
->fields('n', array('tid'))
->condition('n.nexx_item_id', $omniaId)
->condition('n.nexx_item_id', $omnia_id)
->execute();
$nexx_id = $result->fetchField();
return $nexx_id;

$drupal_id = $result->fetchField();

return $drupal_id;
}

/**
Expand Down
104 changes: 103 additions & 1 deletion src/Plugin/MediaEntity/Type/NexxVideo.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,110 @@ public function providedFields() {
/**
* {@inheritdoc}
*/
public function getField(MediaInterface $media, $name) {}
public function getField(MediaInterface $media, $name) {
$video_field = $this->getVideoField($media);

if (empty($video_field)) {
return FALSE;
}
switch ($name) {
case 'nexx_item_id':
if (!empty($media->{$video_field}->item_id)) {
return $media->{$video_field}->item_id;
}
break;

case 'is_ssc':
if (!empty($media->{$video_field}->isSSC)) {
return $media->{$video_field}->isSSC;
}
break;

case 'encoded_ssc':
if (!empty($media->{$video_field}->encodedSSC)) {
return $media->{$video_field}->encodedSSC;
}
break;

case 'encoded_html5':
if (!empty($media->{$video_field}->encodedHTML5)) {
return $media->{$video_field}->encodedHTML5;
}
break;

case 'is_mobile':
if (!empty($media->{$video_field}->isMOBILE)) {
return $media->{$video_field}->isMOBILE;
}
break;

case 'encoded_mobile':
if (!empty($media->{$video_field}->encodedMOBILE)) {
return $media->{$video_field}->encodedMOBILE;
}
break;

case 'is_hyve':
if (!empty($media->{$video_field}->isHYVE)) {
return $media->{$video_field}->isHYVE;
}
break;

case 'encoded_hyve':
if (!empty($media->{$video_field}->encodedHYVE)) {
return $media->{$video_field}->encodedHYVE;
}
break;

case 'deleted':
if (!empty($media->{$video_field}->isDeleted)) {
return $media->{$video_field}->isDeleted;
}
break;

case 'blocked':
if (!empty($media->{$video_field}->isBlocked)) {
return $media->{$video_field}->isBlocked;
}
break;

default:
if (!empty($media->{$video_field}->{$name})) {
return $media->{$video_field}->{$name};
}

}

return FALSE;
}

/**
* Retrieve video field name.
*
* @param MediaInterface $media
* The media object for which the field should be retrieved.
*
* @return string $videoField
* The fieldname of the video field;
*
* @throws \Exception
*/
public function getVideoField(MediaInterface $media) {
$fieldDefinitions = $media->getFieldDefinitions();
foreach ($fieldDefinitions as $field_name => $fieldDefinition) {
if ($fieldDefinition->getType() === 'nexx_video_data') {
$videoField = $field_name;
break;
}
}

if (empty($videoField)) {
throw new \Exception('No video data field defined');
}

return $videoField;
}

/**
* {@inheritdoc}
*/
Expand Down
32 changes: 32 additions & 0 deletions tests/src/Unit/OmniaControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Drupal\Tests\nexx_integration\Unit;

use Drupal\Tests\UnitTestCase;
use Drupal\nexx_integration\Controller\OmniaController;

/**
* Provides automated tests for the nexx integration Omnia controller.
*
* @group nexx_integration
*/
class OmniaControllerTest extends UnitTestCase {

/**
* {@inheritdoc}
*/
public static function getInfo() {
return array(
'name' => "OmniaController functionality",
'description' => 'Test Unit for module nexx_integration\'s omnia controller.',
);
}

/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
}

}

0 comments on commit d6b246d

Please sign in to comment.