11 |
12 | {{ title_prefix }}
13 | {% if not page and not view_mode == 'thumbnail' %}
14 |
15 | {% endif %}
16 | {{ title_suffix }}
17 |
18 |
19 | {{ content }}
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/tests/src/Kernel/FileEntityNormalizerTest.php:
--------------------------------------------------------------------------------
1 | installEntitySchema('node');
54 | $this->installEntitySchema('file');
55 | $this->installEntitySchema('user');
56 | $this->installSchema('file', array('file_usage'));
57 | $this->installSchema('file_entity', array('file_metadata'));
58 |
59 | // Set the file route to provide entity URI for serialization.
60 | $route_collection = new RouteCollection();
61 | $route_collection->add('entity.file.canonical', new Route('file/{file}'));
62 | $this->container->set('router.route_provider', new MockRouteProvider($route_collection));
63 | }
64 |
65 | /**
66 | * Tests that file field is identical before and after de/serialization.
67 | */
68 | public function testFileFieldSerializePersist() {
69 | // Create a node type.
70 | $node_type = NodeType::create(array('type' => $this->randomMachineName()));
71 | $node_type->save();
72 |
73 | // Create a file.
74 | $file_name = $this->randomMachineName() . '.txt';
75 | file_put_contents("public://$file_name", $this->randomString());
76 | $file = File::create(array(
77 | 'uri' => "public://$file_name",
78 | ));
79 | $file->save();
80 |
81 | // Attach a file field to the node type.
82 | $file_field_storage = FieldStorageConfig::create(array(
83 | 'type' => 'file',
84 | 'entity_type' => 'node',
85 | 'field_name' => 'field_file',
86 | ));
87 | $file_field_storage->save();
88 | $file_field_instance = FieldConfig::create(array(
89 | 'field_storage' => $file_field_storage,
90 | 'entity_type' => 'node',
91 | 'bundle' => $node_type->id(),
92 | ));
93 | $file_field_instance->save();
94 |
95 | // Create a node referencing the file.
96 | $node = Node::create(array(
97 | 'title' => 'A node with a file',
98 | 'type' => $node_type->id(),
99 | 'field_file' => array(
100 | 'target_id' => $file->id(),
101 | 'display' => 0,
102 | 'description' => 'An attached file',
103 | ),
104 | 'status' => TRUE,
105 | ));
106 |
107 | // Export.
108 | $serialized = $this->container->get('serializer')->serialize($node, 'hal_json');
109 |
110 | // Import again.
111 | $deserialized = $this->container->get('serializer')->deserialize($serialized, 'Drupal\node\Entity\Node', 'hal_json');
112 |
113 | // Compare.
114 | $this->assertEqual($node->toArray()['field_file'], $deserialized->toArray()['field_file'], "File field persists.");
115 | }
116 |
117 |
118 | /**
119 | * Tests that file entities are correctly serialized, including file contents.
120 | */
121 | public function testFileSerialize() {
122 |
123 | FileType::create(array(
124 | 'id' => 'undefined',
125 | ))->save();
126 | foreach ($this->getTestFiles() as $file_obj) {
127 | $file_contents = file_get_contents($file_obj->uri);
128 |
129 | // Create file entity.
130 | $file = File::create(array(
131 | 'uri' => $file_obj->uri,
132 | 'status' => TRUE,
133 | ));
134 | $file->save();
135 |
136 | // Serialize.
137 | $serialized = $this->container->get('serializer')->serialize($file, 'hal_json');
138 |
139 | // Remove file.
140 | $file->delete();
141 | $this->container->get('entity.manager')->getStorage('file')->resetCache();
142 | $this->assertFalse(file_exists($file_obj->uri), "Deleted file $file_obj->uri from disk");
143 | $this->assertFalse(File::load($file->id()), "Deleted file {$file->id()} entity");
144 |
145 | // Deserialize again.
146 | $deserialized = $this->container->get('serializer')->deserialize($serialized, 'Drupal\file\Entity\File', 'hal_json');
147 | $deserialized->save();
148 |
149 | // Compare.
150 | $files = File::loadMultiple();
151 | $last_file = array_pop($files);
152 | $this->assertNotNull($last_file, 'A file entity was created');
153 | $this->assertTrue(file_exists($file_obj->uri), "A file was created on disk");
154 |
155 | // Assert file is equal.
156 | foreach (array('filename', 'uri', 'filemime', 'filesize', 'type') as $property) {
157 | $this->assertEqual($file->get($property)->value, $last_file->get($property)->value);
158 | }
159 | $this->assertEqual($file->get('type')->target_id, $last_file->get('type')->target_id);
160 | $this->assertEqual($file_contents, file_get_contents($last_file->getFileUri()), 'File contents are equal');
161 | }
162 | }
163 |
164 | /**
165 | * Tests that image field is identical before and after de/serialization.
166 | */
167 | public function testImageFieldSerializePersist() {
168 | // Create a node type.
169 | $node_type = NodeType::create(array('type' => $this->randomMachineName()));
170 | $node_type->save();
171 |
172 | // Create a file.
173 | $file_name = $this->randomMachineName() . '.jpg';
174 | file_put_contents("public://$file_name", $this->randomString());
175 | $image = File::create(array(
176 | 'uri' => "public://$file_name",
177 | ));
178 | $image->save();
179 |
180 | // Attach a file field to the node type.
181 | $image_field_storage = FieldStorageConfig::create(array(
182 | 'type' => 'image',
183 | 'entity_type' => 'node',
184 | 'field_name' => 'field_image',
185 | ));
186 | $image_field_storage->save();
187 | $file_field_instance = FieldConfig::create(array(
188 | 'field_storage' => $image_field_storage,
189 | 'entity_type' => 'node',
190 | 'bundle' => $node_type->id(),
191 | ));
192 | $file_field_instance->save();
193 |
194 | // Create a node referencing the image.
195 | $node = Node::create(array(
196 | 'title' => 'A node with a image',
197 | 'type' => $node_type->id(),
198 | 'field_image' => array(
199 | 'target_id' => $image->id(),
200 | 'alt' => 'the image alternative',
201 | 'title' => 'the title',
202 | 'width' => 50,
203 | 'height' => 50,
204 | ),
205 | 'status' => TRUE,
206 | ));
207 |
208 | // Export.
209 | $serialized = $this->container->get('serializer')->serialize($node, 'hal_json');
210 |
211 | // Import again.
212 | $deserialized = $this->container->get('serializer')->deserialize($serialized, 'Drupal\node\Entity\Node', 'hal_json');
213 |
214 | // Compare.
215 | $this->assertEqual($node->toArray()['field_image'], $deserialized->toArray()['field_image'], "Image field persists.");
216 | }
217 |
218 | /**
219 | * Create some test files like WebTestBase::drupalGetTestFiles().
220 | *
221 | * @return array
222 | * An associative array (keyed on uri) of objects with 'uri', 'filename',
223 | * and 'name' properties corresponding to the test files.
224 | */
225 | protected function getTestFiles() {
226 | $original = drupal_get_path('module', 'simpletest') . '/files';
227 | $files = file_scan_directory($original, '/(html|image|javascript|php|sql)-.*/');
228 | foreach ($files as $file) {
229 | unset($files[$file->uri]);
230 | $new_path = file_unmanaged_copy($file->uri, PublicStream::basePath());
231 | $file->uri = $new_path;
232 | $files[$new_path] = $file;
233 | }
234 | return $files;
235 | }
236 |
237 | }
238 |
--------------------------------------------------------------------------------
/views/views_handler_field_file_rendered.inc:
--------------------------------------------------------------------------------
1 | 'default');
17 | return $options;
18 | }
19 |
20 | /**
21 | * Provide file_view_mode option for to file display.
22 | */
23 | function options_form(&$form, &$form_state) {
24 | parent::options_form($form, $form_state);
25 |
26 | $entity_info = entity_get_info('file');
27 | $options = array('default' => t('Default'));
28 | foreach ($entity_info['view modes'] as $file_view_mode => $file_view_mode_info) {
29 | $options[$file_view_mode] = $file_view_mode_info['label'];
30 | }
31 |
32 | $form['file_view_mode'] = array(
33 | '#title' => t('File view mode'),
34 | '#description' => t('Select a view mode. Note that only the file will be rendered and not any of its fields.'),
35 | '#type' => 'select',
36 | '#default_value' => $this->options['file_view_mode'],
37 | '#options' => $options,
38 | );
39 | }
40 |
41 | function render($values) {
42 | $file = $this->get_value($values);
43 | return file_view_file($file, $this->options['file_view_mode']);
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/views/views_plugin_row_file_rss.inc:
--------------------------------------------------------------------------------
1 | 'default');
25 | $options['links'] = array('default' => FALSE, 'bool' => TRUE);
26 |
27 | return $options;
28 | }
29 |
30 | /**
31 | * Override init function to convert fulltext view-mode to full.
32 | */
33 | function init(&$view, &$display, $options = NULL) {
34 | parent::init($view, $display, $options);
35 |
36 | if ($this->options['item_length'] == 'fulltext') {
37 | $this->options['item_length'] = 'full';
38 | }
39 | }
40 |
41 | function options_form(&$form, &$form_state) {
42 | parent::options_form($form, $form_state);
43 |
44 | $form['item_length'] = array(
45 | '#type' => 'select',
46 | '#title' => t('Display type'),
47 | '#options' => $this->options_form_summary_options(),
48 | '#default_value' => $this->options['item_length'],
49 | );
50 | $form['links'] = array(
51 | '#type' => 'checkbox',
52 | '#title' => t('Display links'),
53 | '#default_value' => $this->options['links'],
54 | );
55 | }
56 |
57 | /**
58 | * Return the main options, which are shown in the summary title.
59 | */
60 | function options_form_summary_options() {
61 | $entity_info = entity_get_info('file');
62 | $options = array();
63 | if (!empty($entity_info['view modes'])) {
64 | foreach ($entity_info['view modes'] as $mode => $settings) {
65 | $options[$mode] = $settings['label'];
66 | }
67 | }
68 | $options['title'] = t('Title only');
69 | $options['default'] = t('Use site default RSS settings');
70 | return $options;
71 | }
72 |
73 | function summary_title() {
74 | $options = $this->options_form_summary_options();
75 | return check_plain($options[$this->options['item_length']]);
76 | }
77 |
78 |
79 | function pre_render($values) {
80 | $fids = array();
81 | foreach ($values as $row) {
82 | $fids[] = $row->{$this->field_alias};
83 | }
84 | if (!empty($fids)) {
85 | $this->files = file_load_multiple($fids);
86 | }
87 | }
88 |
89 | function render($row) {
90 | // For the most part, this code is taken from node_feed() in node.module
91 | global $base_url;
92 |
93 | $fid = $row->{$this->field_alias};
94 | if (!is_numeric($fid)) {
95 | return;
96 | }
97 |
98 | $display_mode = $this->options['item_length'];
99 | if ($display_mode == 'default') {
100 | $display_mode = variable_get('feed_item_length', 'teaser');
101 | }
102 |
103 | // Load the specified file:
104 | $file = $this->files[$fid];
105 | if (empty($file)) {
106 | return;
107 | }
108 |
109 | $item_text = '';
110 |
111 | $uri = entity_uri('file', $file);
112 | $user = user_load($file->uid);
113 | $file->link = url($uri['path'], $uri['options'] + array('absolute' => TRUE));
114 | $file->rss_namespaces = array();
115 | $file->rss_elements = array(
116 | array(
117 | 'key' => 'pubDate',
118 | 'value' => gmdate('r', $file->timestamp),
119 | ),
120 | array(
121 | 'key' => 'dc:creator',
122 | 'value' => $user->name,
123 | ),
124 | array(
125 | 'key' => 'guid',
126 | 'value' => $file->fid . ' at ' . $base_url,
127 | 'attributes' => array('isPermaLink' => 'false'),
128 | ),
129 | );
130 |
131 | // The file gets built and modules add to or modify $file->rss_elements
132 | // and $file->rss_namespaces.
133 |
134 | $build_mode = $display_mode;
135 |
136 | $build = file_view($file, $build_mode);
137 | unset($build['#theme']);
138 |
139 | if (!empty($file->rss_namespaces)) {
140 | $this->view->style_plugin->namespaces = array_merge($this->view->style_plugin->namespaces, $file->rss_namespaces);
141 | }
142 | elseif (function_exists('rdf_get_namespaces')) {
143 | // Merge RDF namespaces in the XML namespaces in case they are used
144 | // further in the RSS content.
145 | $xml_rdf_namespaces = array();
146 | foreach (rdf_get_namespaces() as $prefix => $uri) {
147 | $xml_rdf_namespaces['xmlns:' . $prefix] = $uri;
148 | }
149 | $this->view->style_plugin->namespaces += $xml_rdf_namespaces;
150 | }
151 |
152 | // Hide the links if desired.
153 | if (!$this->options['links']) {
154 | hide($build['links']);
155 | }
156 |
157 | if ($display_mode != 'title') {
158 | // We render file contents and force links to be last.
159 | $build['links']['#weight'] = 1000;
160 | $item_text .= drupal_render($build);
161 | }
162 |
163 | $item = new stdClass();
164 | $item->description = $item_text;
165 | $item->title = $file->filename;
166 | $item->link = $file->link;
167 | $item->elements = $file->rss_elements;
168 | $item->fid = $file->fid;
169 |
170 | return theme($this->theme_functions(), array(
171 | 'view' => $this->view,
172 | 'options' => $this->options,
173 | 'row' => $item
174 | ));
175 | }
176 | }
177 |
--------------------------------------------------------------------------------