-
Notifications
You must be signed in to change notification settings - Fork 1
/
applenews.module
157 lines (141 loc) · 5.53 KB
/
applenews.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
/**
* @file
* Apple News module file.
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\migrate\MigrateSkipRowException;
use Drupal\migrate\Plugin\MigrateSourceInterface;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Row;
/**
* Implements hook_help().
*/
function applenews_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.applenews':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Apple News module provides an easy way to get your Drupal site\'s content into Apple News.') . '</p>';
$output .= '<h3>' . t('Key features') . '</h3>';
$output .= '<ul>';
$output .= '<li>' . t('Build custom Apple News layout.') . '</li>';
$output .= '<li>' . t('Ability to export content to preview in the News Preview application.') . '</li>';
$output .= '<li>' . t('Bulk export to Apple News.') . '</li>';
$output .= '<li>' . t('Export individual content to Apple News.') . '</li>';
$output .= '<li>' . t('Integration with Views allows to build custom reports.') . '</li>';
$output .= '<li>' . t('Built-in plugin system allows extend module components by adding support to almost any field types.') . '</li>';
$output .= '</ul>';
$output .= '<h3>' . t('Dependencies') . '</h3>';
$output .= '<p>' . t('Apple News requires the Serialization module (in Web Services) to be installed.') . '</p>';
$output .= '<h3>' . t('More Help') . '</h3>';
$output .= '<p>' . t('For more help, please consult the README.MD file in the applenews module folder.') . '</p>';
return $output;
}
}
/**
* Implements hook_entity_presave().
*/
function applenews_entity_presave(EntityInterface $entity) {
if (!$entity instanceof FieldableEntityInterface) {
return;
}
/** @var \Drupal\applenews\ApplenewsManager $manager */
$manager = \Drupal::service('applenews.manager');
$manager->entityPreSave($entity);
}
/**
* Implements hook_entity_insert().
*/
function applenews_entity_insert(EntityInterface $entity) {
if (!$entity instanceof FieldableEntityInterface) {
return;
}
/** @var \Drupal\applenews\ApplenewsManager $manager */
$manager = \Drupal::service('applenews.manager');
$manager->entitySave($entity);
}
/**
* Implements hook_entity_update().
*/
function applenews_entity_update(EntityInterface $entity) {
if (!$entity instanceof FieldableEntityInterface) {
return;
}
/** @var \Drupal\applenews\ApplenewsManager $manager */
$manager = \Drupal::service('applenews.manager');
$manager->entitySave($entity);
}
/**
* Implements hook_entity_delete().
*/
function applenews_entity_delete(EntityInterface $entity) {
if (!$entity instanceof FieldableEntityInterface) {
return;
}
try {
/** @var \Drupal\applenews\ApplenewsManager $manager */
$manager = \Drupal::service('applenews.manager');
// On successful post, save response details on entity.
$manager->delete($entity);
}
catch (\Exception $e) {
\Drupal::logger('applenews')->error(t('Error while trying to delete an article in Apple News: @error', ['@err' => $e->getMessage()]));
}
}
/**
* Implements hook_migration_plugins_alter().
*/
function applenews_migration_plugins_alter(array &$migrations) {
// Add field mapping for applenews node fields.
$applenews_manager = \Drupal::service('applenews.manager');
$fields = $applenews_manager->getFields('node');
if (empty($fields)) {
return;
}
foreach ($fields as $field_instance_id => $field_instance_config) {
foreach ($field_instance_config['bundles'] as $bundle) {
if (isset($migrations['d7_node:' . $bundle])) {
// We could map status to publish_flag but that would trigger publishing
// the article to Applenews so we hardcode it to+ 0.
$migrations['d7_node:' . $bundle]['source']['constants']['applenews_status'] = 0;
$migrations['d7_node:' . $bundle]['process'] += [
$field_instance_id . '/status' => 'constants/applenews_status',
$field_instance_id . '/channels' => 'applenews_channels',
$field_instance_id . '/is_preview' => 'applenews_is_preview',
$field_instance_id . '/template' => 'applenews_template',
];
}
}
}
}
/**
* Implements hook_migrate_prepare_row().
*/
function applenews_migrate_prepare_row(
Row $row,
MigrateSourceInterface $source,
MigrationInterface $migration
) {
if ($source->getPluginId() == 'd7_node') {
$data = $source->getDatabase()->query('select data from {applenews_entity_settings} where entity_type = :type and entity_id = :entity_id', [':type' => 'node', ':entity_id' => $row->getSourceProperty('nid')])->fetchField();
if (!empty($data)) {
// Extract channels and is_preview from the data field.
$data = unserialize($data);
$row->setSourceProperty('applenews_channels', serialize($data['channels']));
$row->setSourceProperty('applenews_is_preview', (int) $data['is_preview']);
// Find a template for this node's content type.
$template_ids = \Drupal::entityTypeManager()
->getStorage('applenews_template')
->getQuery()
->condition('node_type', 'blog')
->execute();
if (empty($template_ids)) {
throw new MigrateSkipRowException(sprintf('You need to create an Applenews template for content type %s.', $row->getSourceProperty('type')));
}
$row->setSourceProperty('applenews_template', reset($template_ids));
}
}
}