Skip to content

Commit

Permalink
Merge pull request #88 from anpolimus/master
Browse files Browse the repository at this point in the history
Fix of bugs + code review fixes
  • Loading branch information
hamrant authored Aug 25, 2020
2 parents 14f1c3f + 2f4882b commit 609dbe0
Show file tree
Hide file tree
Showing 19 changed files with 122 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ status: true
dependencies: { }
id: openy
label: 'Open Y'
url: 'http://rose.demo.openy.ci.fivejars.com'
url: 'https://shared.openy.org'
token: ''
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ name: 'Virtual Y Shared Content'
type: module
description: 'Virtual Y Shared Content'
package: Open Y Virtual YMCA
version: 0.1
core: 8.x
core_version_requirement: ^8 || ^9
dependencies:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Drupal\Core\Form\FormStateInterface;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;

Expand Down Expand Up @@ -37,6 +38,13 @@ class SharedContentSourceServerForm extends EntityForm {
*/
protected $config;

/**
* A logger instance.
*
* @var \Psr\Log\LoggerInterface
*/
protected $logger;

/**
* Constructs an SharedContentSource object.
*
Expand All @@ -48,11 +56,14 @@ class SharedContentSourceServerForm extends EntityForm {
* The request stack.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config
* The configuration factory service.
* @param \Psr\Log\LoggerInterface $logger
* A logger instance.
*/
public function __construct(EntityTypeManagerInterface $entityTypeManager, Client $client, RequestStack $request_stack, ConfigFactoryInterface $config) {
public function __construct(EntityTypeManagerInterface $entityTypeManager, Client $client, RequestStack $request_stack, ConfigFactoryInterface $config, LoggerInterface $logger) {
$this->entityTypeManager = $entityTypeManager;
$this->client = $client;
$this->config = $config;
$this->logger = $logger;
$this->request = $request_stack->getCurrentRequest();
}

Expand All @@ -64,7 +75,8 @@ public static function create(ContainerInterface $container) {
$container->get('entity_type.manager'),
$container->get('http_client'),
$container->get('request_stack'),
$container->get('config.factory')
$container->get('config.factory'),
$container->get('logger.factory')->get('openy_gc_shared_content')
);
}

Expand Down Expand Up @@ -167,6 +179,7 @@ protected function requestToken($url) {
}
}
catch (RequestException $e) {
$this->logger->notice($e->getMessage());
return NULL;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
*/
class ShareToVirtualYBase extends FieldUpdateActionBase {

const GC_SHARE_ENABLED = 1;

/**
* {@inheritdoc}
*/
protected function getFieldsToUpdate() {
return ['field_gc_share' => 1];
return ['field_gc_share' => self::GC_SHARE_ENABLED];
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
*/
class UnshareFromVirtualYBase extends FieldUpdateActionBase {

const GC_SHARE_DEFAULT_VALUE = 0;

/**
* {@inheritdoc}
*/
protected function getFieldsToUpdate() {
return ['field_gc_share' => 0];
return ['field_gc_share' => self::GC_SHARE_DEFAULT_VALUE];
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public function formatItem($data, $teaser = TRUE) {
}
if ($item['type'] == 'media--video') {
if ($item['attributes']['field_media_source'] == 'youtube') {
$url = 'http://www.youtube.com/embed/' . $item['attributes']['field_media_video_id'];
$url = 'https://www.youtube.com/embed/' . $item['attributes']['field_media_video_id'];
}
elseif ($item['attributes']['field_media_source'] == 'vimeo') {
$url = 'https://player.vimeo.com/video/' . $item['attributes']['field_media_video_id'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,13 @@ public function saveMediaFromSource($parent_data, $data, $bundle, $url) {
return [];
}
$file_temp = file_get_contents($url . $file_data['attributes']['uri']['url']);
if (!$file_temp) {
return [];
}
$file = file_save_data($file_temp, $file_data['attributes']['uri']['value']);
if (!$file) {
return [];
}
}

unset($data['attributes']['drupal_internal__mid']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ source:
plugin: url
data_fetcher_plugin: http
# Specifies the JSON parser plugin.
data_parser_plugin: json
data_parser_plugin: virtualy_json
track_changes: true
headers:
Accept: 'application/json; charset=utf-8'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ source:
plugin: url
data_fetcher_plugin: http
# Specifies the JSON parser plugin.
data_parser_plugin: json
data_parser_plugin: virtualy_json
track_changes: true
headers:
Accept: 'application/json; charset=utf-8'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ source:
plugin: url
data_fetcher_plugin: http
# Specifies the JSON parser plugin.
data_parser_plugin: json
data_parser_plugin: virtualy_json
track_changes: true
headers:
Accept: 'application/json; charset=utf-8'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ source:
plugin: url
data_fetcher_plugin: http
# Specifies the JSON parser plugin.
data_parser_plugin: json
data_parser_plugin: virtualy_json
track_changes: true
headers:
Accept: 'application/json; charset=utf-8'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ source:
plugin: url
data_fetcher_plugin: http
# Specifies the JSON parser plugin.
data_parser_plugin: json
data_parser_plugin: virtualy_json
track_changes: true
headers:
Accept: 'application/json; charset=utf-8'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ source:
plugin: url
data_fetcher_plugin: http
# Specifies the JSON parser plugin.
data_parser_plugin: json
data_parser_plugin: virtualy_json
track_changes: true
headers:
Accept: 'application/json; charset=utf-8'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ source:
track_changes: true
data_fetcher_plugin: http
# Specifies the JSON parser plugin.
data_parser_plugin: json
data_parser_plugin: virtualy_json
headers:
Accept: 'application/json; charset=utf-8'
Content-Type: 'application/json'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ source:
plugin: url
data_fetcher_plugin: http
# Specifies the JSON parser plugin.
data_parser_plugin: json
data_parser_plugin: virtualy_json
track_changes: true
headers:
Accept: 'application/json; charset=utf-8'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ source:
plugin: url
data_fetcher_plugin: http
# Specifies the JSON parser plugin.
data_parser_plugin: json
data_parser_plugin: virtualy_json
track_changes: true
headers:
Accept: 'application/json; charset=utf-8'
Expand Down Expand Up @@ -68,6 +68,9 @@ process:
default_value: 1
title: title
field_gc_origin: constants/DOMAIN
field_gc_share:
plugin: default_value
default_value: 1
field_vy_blog_description/value: field_vy_blog_description
field_vy_blog_description/format:
plugin: default_value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ source:
plugin: url
data_fetcher_plugin: http
# Specifies the JSON parser plugin.
data_parser_plugin: json
data_parser_plugin: virtualy_json
track_changes: true
headers:
Accept: 'application/json; charset=utf-8'
Expand Down Expand Up @@ -84,6 +84,9 @@ process:
default_value: 1
title: title
field_gc_origin: constants/DOMAIN
field_gc_share:
plugin: default_value
default_value: 1
field_gc_video_description/value: field_gc_video_description
field_gc_video_description/format:
plugin: default_value
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Drupal\openy_gc_shared_content_server\Plugin\migrate_plus\data_parser;

use Drupal\migrate_plus\Plugin\migrate_plus\data_parser\Json;

/**
* Obtain JSON data for migration.
*
* @DataParser(
* id = "virtualy_json",
* title = @Translation("Virtual Y JSON")
* )
*/
class VirtualYJson extends Json {

/**
* Retrieves the JSON data and returns it as an array.
*
* @param string $url
* URL of a JSON feed.
*
* @return array
* The selected data to be iterated.
*
* @throws \GuzzleHttp\Exception\RequestException
*/
protected function getSourceData($url) {
$response = $this->getDataFetcherPlugin()->getResponseContent($url);

// Convert objects to associative arrays.
$source_data = json_decode($response, TRUE);

// If json_decode() has returned NULL, it might be that the data isn't
// valid utf8 - see http://php.net/manual/en/function.json-decode.php#86997.
if (is_null($source_data)) {
$utf8response = utf8_encode($response);
$source_data = json_decode($utf8response, TRUE);
}

// Backwards-compatibility for depth selection.
if (is_int($this->itemSelector)) {
return $this->selectByDepth($source_data);
}

// Otherwise, we're using xpath-like selectors.
$selectors = explode('/', trim($this->itemSelector, '/'));
foreach ($selectors as $selector) {
if (!empty($selector)) {
if (!empty($source_data[$selector])) {
$source_data = $source_data[$selector];
}
else {
$source_data = [];
}
}
}

return $source_data;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Component\Plugin\Derivative\DeriverInterface;
use Drupal\Core\Entity\Query\QueryFactory;
use Drupal\Core\Entity\EntityTypeManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\openy_gc_shared_content_server\Entity\SharedContentSource;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
Expand All @@ -19,26 +19,31 @@ class SourceMigrationDeriver extends DeriverBase implements DeriverInterface, Co
/**
* EntityQuery service instance.
*
* @var \Drupal\Core\Entity\Query\QueryFactory
* @var \Drupal\Core\Entity\Query\QueryInterface
*/
protected $entityQuery;
protected $sharedContentStorage;

/**
* SourceMigrationDeriver constructor.
*
* @param \Drupal\Core\Entity\Query\QueryFactory $entityQuery
* EntityQuery instance.
* @param \Drupal\Core\Entity\EntityTypeManager $entityTypeManager
* EntityTypeManager service instance.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
public function __construct(QueryFactory $entityQuery) {
$this->entityQuery = $entityQuery;
public function __construct(EntityTypeManager $entityTypeManager) {
$this->sharedContentStorage = $entityTypeManager
->getStorage('shared_content_source')
->getQuery();
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, $base_plugin_id) {
return new static(
$container->get('entity.query')
$container->get('entity_type.manager')
);
}

Expand All @@ -47,7 +52,7 @@ public static function create(ContainerInterface $container, $base_plugin_id) {
*/
public function getDerivativeDefinitions($base_plugin_definition) {

$ids = $this->entityQuery->get('shared_content_source')->execute();
$ids = $this->sharedContentStorage->execute();

if (empty($ids)) {
return [];
Expand Down

0 comments on commit 609dbe0

Please sign in to comment.