/**
  * Helper function to create our different paragraph types.
  *
  * @param  [type] $paragraph [description]
  * @return [type]            [description]
  */
 private function createParagraphItem($paragraph)
 {
     // Default data for all paragraph types
     $data = ['type' => $paragraph['type']];
     // Every paragraph type might add specific data
     switch ($paragraph['type']) {
         case 'cr_rich_text_paragraph':
             $data['field_body'] = ['value' => $paragraph['body'], 'format' => 'basic_html'];
             $data['field_background'] = $this->expandImage($paragraph['image']);
             break;
         case 'cr_single_message_row':
             $data['field_single_msg_row_lr_title'] = ['value' => $paragraph['title']];
             $data['field_single_msg_row_lr_variant'] = ['value' => $paragraph['variant']];
             $data['field_single_msg_row_lr_body'] = ['value' => $paragraph['body'], 'format' => 'basic_html'];
             $data['field_single_msg_row_lr_image'] = $this->expandImage($paragraph['image']);
             break;
     }
     $paragraph_item = \Drupal\paragraphs\Entity\Paragraph::create($data);
     $paragraph_item->save();
     return $paragraph_item;
 }
Exemple #2
0
 /**
  * Create default content for entity
  * 
  * @param $content, $type
  *  array of content information
  *  entity type
  *
  * @return
  *  paragraph object id
  */
 public function createDefaultContentForEntity($content, $type)
 {
     $entity = NULL;
     $theme_array = $this->getCustomTheme();
     $theme_name = array_keys($theme_array)[0];
     $file_directory = 'default';
     switch ($type) {
         case 'component':
             $entity = Component::create(['type' => $content['id'], 'name' => ucwords(str_replace('_', ' ', $content['id']))]);
             $entity->save();
             $file_directory = 'components/' . str_replace('_', ' ', $content['id']);
             break;
         case 'paragraph':
             $entity = Paragraph::create(['type' => $content['id'], 'title' => ucwords(str_replace('_', ' ', $content['id']))]);
             $entity->save();
             $file_directory = 'paragraphs/' . str_replace('_', ' ', $content['id']);
             break;
     }
     foreach ($content['fields'] as $field) {
         if ($field['field_type'] == 'image' || $field['field_type'] == 'file') {
             $settings['file_directory'] = $file_directory . '/[date:custom:Y]-[date:custom:m]';
             $uri = drupal_get_path('theme', $theme_name) . '/' . $field['value'];
             if (file_exists($uri) && !is_dir($uri)) {
                 $image = File::create();
                 $image->setFileUri($uri);
                 $image->setOwnerId(\Drupal::currentUser()->id());
                 $image->setMimeType('image/' . pathinfo($field['value'], PATHINFO_EXTENSION));
                 $image->setFileName(drupal_basename($field['value']));
                 $destination_dir = 'public://' . $file_directory;
                 file_prepare_directory($destination_dir, FILE_CREATE_DIRECTORY);
                 $destination = $destination_dir . '/' . basename($field['value']);
                 $file = file_copy($image, $destination);
                 $values = array('target_id' => $file->id());
                 $entity->set($field['field_name'], $values);
             }
         } else {
             $entity->set($field['field_name'], $field['value']);
         }
     }
     $entity->save();
     \Drupal::logger('clutch:workflow')->notice('Create content for type @type - bundle @bundle', array('@type' => $type, '@bundle' => $content['id']));
     return $entity;
 }
 /**
  * Tests the revision of paragraphs.
  */
 public function testParagraphsRevisions()
 {
     // Create the paragraph type.
     $paragraph_type = ParagraphsType::create(array('label' => 'test_text', 'id' => 'test_text'));
     $paragraph_type->save();
     $paragraph_type_nested = ParagraphsType::create(array('label' => 'test_nested', 'id' => 'test_nested'));
     $paragraph_type_nested->save();
     // Add a paragraph field to the article.
     $field_storage = FieldStorageConfig::create(array('field_name' => 'nested_paragraph_field', 'entity_type' => 'paragraph', 'type' => 'entity_reference_revisions', 'cardinality' => '-1', 'settings' => array('target_type' => 'paragraph')));
     $field_storage->save();
     $field = FieldConfig::create(array('field_storage' => $field_storage, 'bundle' => 'test_nested'));
     $field->save();
     // Add a paragraph field to the article.
     $field_storage = FieldStorageConfig::create(array('field_name' => 'node_paragraph_field', 'entity_type' => 'node', 'type' => 'entity_reference_revisions', 'cardinality' => '-1', 'settings' => array('target_type' => 'paragraph')));
     $field_storage->save();
     $field = FieldConfig::create(array('field_storage' => $field_storage, 'bundle' => 'article'));
     $field->save();
     // Add a paragraph field to the user.
     $field_storage = FieldStorageConfig::create(array('field_name' => 'user_paragraph_field', 'entity_type' => 'user', 'type' => 'entity_reference_revisions', 'settings' => array('target_type' => 'paragraph')));
     $field_storage->save();
     $field = FieldConfig::create(array('field_storage' => $field_storage, 'bundle' => 'user'));
     $field->save();
     // Create a paragraph.
     $paragraph1 = Paragraph::create(['title' => 'Paragraph', 'type' => 'test_text']);
     $paragraph1->save();
     // Create another paragraph.
     $paragraph2 = Paragraph::create(['title' => 'Paragraph', 'type' => 'test_text']);
     $paragraph2->save();
     // Create another paragraph.
     $paragraph3 = Paragraph::create(['title' => 'Paragraph', 'type' => 'test_text']);
     $paragraph3->save();
     // Create another paragraph.
     $paragraph_nested_children1 = Paragraph::create(['title' => 'Paragraph', 'type' => 'test_text']);
     $paragraph_nested_children1->save();
     // Create another paragraph.
     $paragraph_nested_children2 = Paragraph::create(['title' => 'Paragraph', 'type' => 'test_text']);
     $paragraph_nested_children2->save();
     // Create another paragraph.
     $paragraph4_nested_parent = Paragraph::create(['title' => 'Paragraph', 'type' => 'test_nested', 'nested_paragraph_field' => [$paragraph_nested_children1, $paragraph_nested_children2]]);
     $paragraph4_nested_parent->save();
     // Create another paragraph.
     $paragraph_user_1 = Paragraph::create(['title' => 'Paragraph', 'type' => 'test_text']);
     $paragraph_user_1->save();
     // Create a node with two paragraphs.
     $node = Node::create(['title' => $this->randomMachineName(), 'type' => 'article', 'node_paragraph_field' => array($paragraph1, $paragraph2, $paragraph3, $paragraph4_nested_parent)]);
     $node->save();
     // Create an user with a paragraph.
     $user = User::create(['name' => 'test', 'user_paragraph_field' => $paragraph_user_1]);
     $user->save();
     $settings = Settings::getAll();
     $settings['paragraph_limit'] = 1;
     new Settings($settings);
     // Unset the parent field name, type and id of paragraph1.
     /** @var \Drupal\paragraphs\Entity\Paragraph $paragraph */
     $paragraph = Paragraph::load($paragraph1->id());
     $paragraph->set('parent_field_name', NULL);
     $paragraph->set('parent_type', NULL);
     $paragraph->set('parent_id', NULL);
     $paragraph->setNewRevision(FALSE);
     $paragraph->save();
     // Unset the parent field name, type and id of paragraph2.
     $paragraph = Paragraph::load($paragraph2->id());
     $paragraph->set('parent_field_name', NULL);
     $paragraph->set('parent_type', NULL);
     $paragraph->set('parent_id', NULL);
     $paragraph->setNewRevision(FALSE);
     $paragraph->save();
     // Unset the parent field name, type and id of $paragraph_nested_parent.
     $paragraph = Paragraph::load($paragraph4_nested_parent->id());
     $paragraph->set('parent_field_name', NULL);
     $paragraph->set('parent_type', NULL);
     $paragraph->set('parent_id', NULL);
     $paragraph->setNewRevision(FALSE);
     $paragraph->save();
     // Unset the parent field name, type and id of $paragraph_nested_children1.
     $paragraph = Paragraph::load($paragraph_nested_children1->id());
     $paragraph->set('parent_field_name', NULL);
     $paragraph->set('parent_type', NULL);
     $paragraph->set('parent_id', NULL);
     $paragraph->setNewRevision(FALSE);
     $paragraph->save();
     // Unset the parent field name, type and id of paragraph_user_1.
     /** @var \Drupal\paragraphs\Entity\Paragraph $paragraph */
     $paragraph = Paragraph::load($paragraph_user_1->id());
     $paragraph->set('parent_field_name', NULL);
     $paragraph->set('parent_type', NULL);
     $paragraph->set('parent_id', NULL);
     $paragraph->setNewRevision(FALSE);
     $paragraph->save();
     // Create a revision for node.
     /** @var \Drupal\node\Entity\Node $node_revision1 */
     $node_revision1 = Node::load($node->id());
     /** @var \Drupal\paragraphs\Entity\Paragraph $paragraph1_revision1 */
     $paragraph1_revision1 = Paragraph::load($paragraph1->id());
     $paragraph1_revision1->setNewRevision(TRUE);
     $paragraph1_revision1->save();
     /** @var \Drupal\paragraphs\Entity\Paragraph $paragraph2_revision1 */
     $paragraph2_revision1 = Paragraph::load($paragraph2->id());
     $paragraph2_revision1->setNewRevision(TRUE);
     $paragraph2_revision1->save();
     $node_revision1->set('node_paragraph_field', [$paragraph1_revision1, $paragraph2_revision1]);
     $node_revision1->setNewRevision(TRUE);
     $node_revision1->save();
     // Unset the parent field name, type and id of paragraph2_revision1.
     $paragraph2_revision1 = Paragraph::load($paragraph2_revision1->id());
     $paragraph2_revision1->set('parent_field_name', NULL);
     $paragraph2_revision1->set('parent_type', NULL);
     $paragraph2_revision1->set('parent_id', NULL);
     $paragraph2_revision1->setNewRevision(FALSE);
     $paragraph2_revision1->save();
     // Create another revision for node.
     /** @var \Drupal\node\Entity\Node $node_revision2 */
     $node_revision2 = Node::load($node->id());
     /** @var \Drupal\paragraphs\Entity\Paragraph $paragraph1_revision2 */
     $paragraph1_revision2 = Paragraph::load($paragraph1->id());
     $paragraph1_revision2->setNewRevision(TRUE);
     $paragraph1_revision2->save();
     $node_revision2->set('node_paragraph_field', [$paragraph1_revision2]);
     $node_revision2->setNewRevision(TRUE);
     $node_revision2->save();
     // Deletion of referenced paragraphs should not break updates.
     $paragraph3->delete();
     \Drupal::moduleHandler()->loadInclude('paragraphs', 'post_update.php');
     // Run update function and check #finished.
     $sandbox = [];
     do {
         paragraphs_post_update_set_paragraphs_parent_fields($sandbox);
     } while ($sandbox['#finished'] < 1);
     $node_paragraph1 = Paragraph::load($paragraph1->id())->toArray();
     // Check if the fields are properly set.
     self::assertEquals($node_paragraph1['parent_id'][0]['value'], $node->id());
     self::assertEquals($node_paragraph1['parent_type'][0]['value'], $node->getEntityTypeId());
     self::assertEquals($node_paragraph1['parent_field_name'][0]['value'], 'node_paragraph_field');
     $paragraph1_revision1 = \Drupal::entityTypeManager()->getStorage('paragraph')->loadRevision($paragraph1_revision1->getRevisionId())->toArray();
     self::assertEquals($paragraph1_revision1['parent_id'][0]['value'], $node->id());
     self::assertEquals($paragraph1_revision1['parent_type'][0]['value'], $node->getEntityTypeId());
     self::assertEquals($paragraph1_revision1['parent_field_name'][0]['value'], 'node_paragraph_field');
     $paragraph1_revision2 = \Drupal::entityTypeManager()->getStorage('paragraph')->loadRevision($paragraph1_revision2->getRevisionId())->toArray();
     self::assertEquals($paragraph1_revision2['parent_id'][0]['value'], $node->id());
     self::assertEquals($paragraph1_revision2['parent_type'][0]['value'], $node->getEntityTypeId());
     self::assertEquals($paragraph1_revision2['parent_field_name'][0]['value'], 'node_paragraph_field');
     $node_paragraph2 = Paragraph::load($paragraph2->id())->toArray();
     // Check if the fields are properly set.
     self::assertEquals($node_paragraph2['parent_id'][0]['value'], $node->id());
     self::assertEquals($node_paragraph2['parent_type'][0]['value'], $node->getEntityTypeId());
     self::assertEquals($node_paragraph2['parent_field_name'][0]['value'], 'node_paragraph_field');
     $user_paragraph = Paragraph::load($paragraph_user_1->id())->toArray();
     // Check if the fields are properly set.
     self::assertEquals($user_paragraph['parent_id'][0]['value'], $user->id());
     self::assertEquals($user_paragraph['parent_type'][0]['value'], $user->getEntityTypeId());
     self::assertEquals($user_paragraph['parent_field_name'][0]['value'], 'user_paragraph_field');
     $nested_paragraph_parent = Paragraph::load($paragraph4_nested_parent->id())->toArray();
     // Check if the fields are properly set.
     self::assertEquals($nested_paragraph_parent['parent_id'][0]['value'], $node->id());
     self::assertEquals($nested_paragraph_parent['parent_type'][0]['value'], $node->getEntityTypeId());
     self::assertEquals($nested_paragraph_parent['parent_field_name'][0]['value'], 'node_paragraph_field');
     $nested_paragraph_children = Paragraph::load($paragraph_nested_children1->id())->toArray();
     // Check if the fields are properly set.
     self::assertEquals($nested_paragraph_children['parent_id'][0]['value'], $paragraph4_nested_parent->id());
     self::assertEquals($nested_paragraph_children['parent_type'][0]['value'], $paragraph4_nested_parent->getEntityTypeId());
     self::assertEquals($nested_paragraph_children['parent_field_name'][0]['value'], 'nested_paragraph_field');
 }
 /**
  * Tests the paragraph translation.
  */
 public function testParagraphTranslation()
 {
     $this->drupalGet('admin/config/regional/content-language');
     // Check the settings are saved correctly.
     $this->assertFieldChecked('edit-entity-types-paragraph');
     $this->assertFieldChecked('edit-settings-node-paragraphed-content-demo-translatable');
     $this->assertFieldChecked('edit-settings-paragraph-text-image-translatable');
     $this->assertFieldChecked('edit-settings-paragraph-images-columns-field-images-demo-alt');
     $this->assertFieldChecked('edit-settings-paragraph-images-columns-field-images-demo-title');
     // Check if the publish/unpublish option works.
     $this->drupalGet('admin/structure/paragraphs_type/text_image/form-display');
     $edit = array('fields[status][type]' => 'boolean_checkbox');
     $this->drupalPostForm(NULL, $edit, t('Save'));
     $this->drupalGet('node/add/paragraphed_content_demo');
     $this->drupalPostForm(NULL, NULL, t('Add Text + Image'));
     $this->assertRaw('edit-field-paragraphs-demo-0-subform-status-value');
     $edit = ['title[0][value]' => 'example_publish_unpublish', 'field_paragraphs_demo[0][subform][field_text_demo][0][value]' => 'Example published and unpublished'];
     $this->drupalPostForm(NULL, $edit, t('Save and publish'));
     $this->assertText(t('Example published and unpublished'));
     $this->clickLink(t('Edit'));
     $this->drupalPostAjaxForm(NULL, NULL, 'field_paragraphs_demo_nested_paragraph_add_more');
     $this->drupalPostAjaxForm(NULL, NULL, 'field_paragraphs_demo_1_subform_field_paragraphs_demo_text_add_more');
     $edit = ['field_paragraphs_demo[0][subform][status][value]' => FALSE, 'field_paragraphs_demo[1][subform][field_paragraphs_demo][0][subform][field_text_demo][0][value]' => 'Dummy text'];
     $this->drupalPostForm(NULL, $edit, t('Save and keep published'));
     $this->assertNoText(t('Example published and unpublished'));
     // Check the parent fields are set properly. Get the node.
     $node = $this->drupalGetNodeByTitle('example_publish_unpublish');
     // Loop over the paragraphs of the node.
     foreach ($node->field_paragraphs_demo->referencedEntities() as $paragraph) {
         $node_paragraph = Paragraph::load($paragraph->id())->toArray();
         // Check if the fields are set properly.
         $this->assertEqual($node_paragraph['parent_id'][0]['value'], $node->id());
         $this->assertEqual($node_paragraph['parent_type'][0]['value'], 'node');
         $this->assertEqual($node_paragraph['parent_field_name'][0]['value'], 'field_paragraphs_demo');
         // If the paragraph is nested type load the child.
         if ($node_paragraph['type'][0]['target_id'] == 'nested_paragraph') {
             $nested_paragraph = Paragraph::load($node_paragraph['field_paragraphs_demo'][0]['target_id'])->toArray();
             // Check if the fields are properly set.
             $this->assertEqual($nested_paragraph['parent_id'][0]['value'], $paragraph->id());
             $this->assertEqual($nested_paragraph['parent_type'][0]['value'], 'paragraph');
             $this->assertEqual($nested_paragraph['parent_field_name'][0]['value'], 'field_paragraphs_demo');
         }
     }
     // Add paragraphed content.
     $this->drupalGet('node/add/paragraphed_content_demo');
     $this->drupalPostForm(NULL, NULL, t('Add Text + Image'));
     $edit = array('title[0][value]' => 'Title in english', 'field_paragraphs_demo[0][subform][field_text_demo][0][value]' => 'Text in english');
     // The button to remove a paragraph is present.
     $this->assertRaw(t('Remove'));
     $this->drupalPostForm(NULL, $edit, t('Save and publish'));
     $node = $this->drupalGetNodeByTitle('Title in english');
     // The text is present when editing again.
     $this->clickLink(t('Edit'));
     $this->assertText('Title in english');
     $this->assertText('Text in english');
     // Add french translation.
     $this->clickLink(t('Translate'));
     $this->clickLink(t('Add'), 1);
     // Make sure the Add / Remove paragraph buttons are hidden.
     $this->assertNoRaw(t('Remove'));
     $this->assertNoRaw(t('Add Text + Image'));
     // Make sure that the original paragraph text is displayed.
     $this->assertText('Text in english');
     $edit = array('title[0][value]' => 'Title in french', 'field_paragraphs_demo[0][subform][field_text_demo][0][value]' => 'Text in french', 'revision' => TRUE, 'revision_log[0][value]' => 'french 1');
     $this->drupalPostForm(NULL, $edit, t('Save and keep published (this translation)'));
     $this->assertText('Paragraphed article Title in french has been updated.');
     // Check the english translation.
     $this->drupalGet('node/' . $node->id());
     $this->assertText('Title in english');
     $this->assertText('Text in english');
     $this->assertNoText('Title in french');
     $this->assertNoText('Text in french');
     // Check the french translation.
     $this->drupalGet('fr/node/' . $node->id());
     $this->assertText('Title in french');
     $this->assertText('Text in french');
     $this->assertNoText('Title in english');
     // The translation is still present when editing again.
     $this->clickLink(t('Edit'));
     $this->assertText('Title in french');
     $this->assertText('Text in french');
     $edit = array('title[0][value]' => 'Title Change in french', 'field_paragraphs_demo[0][subform][field_text_demo][0][value]' => 'New text in french', 'revision' => TRUE, 'revision_log[0][value]' => 'french 2');
     $this->drupalPostForm(NULL, $edit, t('Save and keep published (this translation)'));
     $this->assertText('Title Change in french');
     $this->assertText('New text in french');
     // Back to the source language.
     $this->drupalGet('node/' . $node->id());
     $this->clickLink(t('Edit'));
     $this->assertText('Title in english');
     $this->assertText('Text in english');
     // Save the original content on second request.
     $this->drupalPostForm(NULL, NULL, t('Save and keep published (this translation)'));
     $this->assertText('Paragraphed article Title in english has been updated.');
     // Test if reverting to old paragraphs revisions works, make sure that
     // the reverted node can be saved again.
     $this->drupalGet('fr/node/' . $node->id() . '/revisions');
     $this->clickLink(t('Revert'));
     $this->drupalPostForm(NULL, ['revert_untranslated_fields' => TRUE], t('Revert'));
     $this->clickLink(t('Edit'));
     $this->assertRaw('Title in french');
     $this->assertText('Text in french');
     $this->drupalPostForm(NULL, [], t('Save and keep published (this translation)'));
     $this->assertNoRaw('The content has either been modified by another user, or you have already submitted modifications');
     $this->assertText('Text in french');
     //Add paragraphed content with untranslatable language
     $this->drupalGet('node/add/paragraphed_content_demo');
     $edit = array('langcode[0][value]' => LanguageInterface::LANGCODE_NOT_SPECIFIED);
     $this->drupalPostForm(NULL, $edit, t('Add Text + Image'));
     $this->assertResponse(200);
     // Make 'Images' paragraph field translatable, enable alt and title fields.
     $this->drupalGet('admin/structure/paragraphs_type/images/fields');
     $this->clickLink('Edit');
     $edit = ['translatable' => 1, 'settings[alt_field]' => 1, 'settings[title_field]' => 1];
     $this->drupalPostForm(NULL, $edit, t('Save settings'));
     // Create a node with an image paragraph, its alt and title text.
     $text = 'Trust me I\'m an image';
     file_put_contents('temporary://Image.jpg', $text);
     $file_path = $this->container->get('file_system')->realpath('temporary://Image.jpg');
     $this->drupalGet('node/add/paragraphed_content_demo');
     $this->drupalPostForm(NULL, [], t('Add Images'));
     $this->drupalPostForm(NULL, ['files[field_paragraphs_demo_0_subform_field_images_demo_0][]' => $file_path], t('Upload'));
     $edit = ['title[0][value]' => 'Title EN', 'field_paragraphs_demo[0][subform][field_images_demo][0][alt]' => 'Image alt', 'field_paragraphs_demo[0][subform][field_images_demo][0][title]' => 'Image title', 'field_paragraphs_demo[0][subform][field_images_demo][0][width]' => 100, 'field_paragraphs_demo[0][subform][field_images_demo][0][height]' => 100];
     $this->drupalPostForm(NULL, $edit, t('Save and publish'));
     // Translate the node with the image paragraph.
     $this->clickLink('Translate');
     $this->clickLink(t('Add'), 1);
     $edit = ['title[0][value]' => 'Title FR', 'field_paragraphs_demo[0][subform][field_images_demo][0][alt]' => 'Image alt FR', 'field_paragraphs_demo[0][subform][field_images_demo][0][title]' => 'Image title FR'];
     $this->drupalPostForm(NULL, $edit, t('Save and keep published (this translation)'));
     $this->assertRaw('Title FR');
     $this->drupalGet('node/add/paragraphed_content_demo');
     $this->drupalPostForm(NULL, [], t('Add Text'));
     $edit = ['field_paragraphs_demo[0][subform][field_text_demo][0][value]' => 'texto', 'title[0][value]' => 'titulo', 'langcode[0][value]' => 'de'];
     $this->drupalPostForm(NULL, $edit, t('Save and publish'));
     $node = $this->drupalGetNodeByTitle('titulo');
     $this->assertParagraphsLangcode($node->id(), 'de');
     // Test langcode matching when Paragraphs and node have different language.
     $paragraph_1 = Paragraph::create(['title' => 'Paragraph', 'type' => 'text', 'langcode' => 'en', 'field_text_demo' => 'english_text_1']);
     $paragraph_1->save();
     $paragraph_2 = Paragraph::create(['title' => 'Paragraph', 'type' => 'text', 'langcode' => 'en', 'field_text_demo' => 'english_text_2']);
     $paragraph_2->save();
     $paragraph_data = $paragraph_2->toArray();
     $paragraph_data['field_text_demo'] = 'german_text_2';
     $paragraph_2->addTranslation('de', $paragraph_data);
     $paragraph_2->save();
     $translated_paragraph = $paragraph_2->getTranslation('en');
     $node = $this->createNode(['langcode' => 'de', 'type' => 'paragraphed_content_demo', 'field_paragraphs_demo' => [$paragraph_1, $translated_paragraph]]);
     $this->drupalGet('node/' . $node->id() . '/edit');
     $this->drupalPostForm(NULL, [], t('Save and keep published'));
     $this->assertText('Paragraphed article ' . $node->label() . ' has been updated.');
     // Check that first paragraph langcode has been updated.
     $paragraph = Paragraph::load($paragraph_1->id());
     $this->assertEqual($paragraph->language()->getId(), 'de');
     $this->assertFalse($paragraph->hasTranslation('en'));
     // Check that second paragraph has two translations.
     $paragraph = Paragraph::load($paragraph_2->id());
     $this->assertTrue($paragraph->hasTranslation('de'));
     $this->assertTrue($paragraph->hasTranslation('en'));
     $this->assertRaw('german_text');
     // Create an english translation of the node.
     $edit = ['field_paragraphs_demo[0][subform][field_text_demo][0][value]' => 'english_translation_1', 'field_paragraphs_demo[1][subform][field_text_demo][0][value]' => 'english_translation_2'];
     $this->drupalPostForm('node/' . $node->id() . '/translations/add/de/en', $edit, t('Save and keep published (this translation)'));
     // Attempt to create a french translation.
     $this->drupalGet('node/' . $node->id() . '/translations/add/de/fr');
     // Check that the german translation of the paragraphs is displayed.
     $this->assertFieldByName('field_paragraphs_demo[0][subform][field_text_demo][0][value]', 'english_text_1');
     $this->assertFieldByName('field_paragraphs_demo[1][subform][field_text_demo][0][value]', 'german_text_2');
     $this->drupalPostForm(NULL, ['source_langcode[source]' => 'en'], t('Change'));
     // Check that the english translation of the paragraphs is displayed.
     $this->assertFieldByName('field_paragraphs_demo[0][subform][field_text_demo][0][value]', 'english_translation_1');
     $this->assertFieldByName('field_paragraphs_demo[1][subform][field_text_demo][0][value]', 'english_translation_2');
     // Create a node with empty Paragraphs.
     $this->drupalGet('node/add/paragraphed_content_demo');
     $this->drupalPostForm(NULL, [], t('Add Nested Paragraph'));
     $this->drupalPostForm(NULL, ['title[0][value]' => 'empty_node'], t('Save and publish'));
     // Attempt to translate it.
     $this->clickLink(t('Translate'));
     $this->clickLink(t('Add'));
     // Check the add button is not displayed.
     $this->assertEqual(count($this->xpath('//*[@name="field_paragraphs_demo_0_subform_field_paragraphs_demo_images_add_more"]')), 0);
     // Add a non translatable field to Text Paragraph type.
     $edit = ['new_storage_type' => 'text_long', 'label' => 'untranslatable_field', 'field_name' => 'untranslatable_field'];
     $this->drupalPostForm('admin/structure/paragraphs_type/text/fields/add-field', $edit, t('Save and continue'));
     $this->drupalPostForm(NULL, [], t('Save field settings'));
     $this->drupalPostForm(NULL, [], t('Save settings'));
     // Add a non translatable reference field.
     $edit = ['new_storage_type' => 'field_ui:entity_reference:node', 'label' => 'untranslatable_ref_field', 'field_name' => 'untranslatable_ref_field'];
     $this->drupalPostForm('admin/structure/paragraphs_type/text/fields/add-field', $edit, t('Save and continue'));
     $this->drupalPostForm(NULL, [], t('Save field settings'));
     $this->drupalPostForm(NULL, ['settings[handler_settings][target_bundles][paragraphed_content_demo]' => TRUE], t('Save settings'));
     // Add a non translatable link field.
     $edit = ['new_storage_type' => 'link', 'label' => 'untranslatable_link_field', 'field_name' => 'untranslatable_link_field'];
     $this->drupalPostForm('admin/structure/paragraphs_type/text/fields/add-field', $edit, t('Save and continue'));
     $this->drupalPostForm(NULL, [], t('Save field settings'));
     $this->drupalPostForm(NULL, [], t('Save settings'));
     // Attempt to add a translation.
     $this->drupalGet('node/' . $node->id() . '/translations/add/de/fr');
     $this->assertText('untranslatable_field (all languages)');
     $this->assertText('untranslatable_ref_field (all languages)');
     $this->assertText('untranslatable_link_field (all languages)');
     $this->assertNoText('Text (all languages)');
     // Enable translations for the reference and link field.
     $edit = ['translatable' => TRUE];
     $this->drupalPostForm('admin/structure/paragraphs_type/text/fields/paragraph.text.field_untranslatable_ref_field', $edit, t('Save settings'));
     $this->drupalPostForm('admin/structure/paragraphs_type/text/fields/paragraph.text.field_untranslatable_link_field', $edit, t('Save settings'));
     // Attempt to add a translation.
     $this->drupalGet('node/' . $node->id() . '/translations/add/de/fr');
     $this->assertText('untranslatable_field (all languages)');
     $this->assertNoText('untranslatable_link_field (all languages)');
     $this->assertNoText('untranslatable_ref_field (all languages)');
     $this->assertNoText('Text (all languages)');
 }
Exemple #5
-1
 /**
  * Create associated components for page
  *
  * @param $component
  *   component string with component type and region
  *
  * @return
  *   array of target_id, target_revision_id of component to associate to page
  */
 public function createAssociatedComponent($component)
 {
     $component_info_array = explode('|', $component);
     $component_type = $component_info_array[0];
     $component_position = $component_info_array[1];
     $component_id = \Drupal::entityQuery('component')->condition('type', $component_type)->execute();
     $paragraph = Paragraph::create(['type' => 'associated_component', 'component' => ['target_id' => key($component_id)], 'region' => ['value' => !empty($component_position) ? $component_position : 'content', 'format' => 'string']]);
     $paragraph->save();
     return ['target_id' => $paragraph->id(), 'target_revision_id' => $paragraph->getRevisionId()];
 }