diff --git a/src/Entity/Paragraph.php b/src/Entity/Paragraph.php index c6fe132..56b3e4a 100644 --- a/src/Entity/Paragraph.php +++ b/src/Entity/Paragraph.php @@ -83,6 +83,9 @@ class Paragraph extends ContentEntityBase implements ParagraphInterface { * {@inheritdoc} */ public function getParentEntity() { + if (!isset($this->get('parent_type')->value) || !isset($this->get('parent_id')->value)) { + return NULL; + } return \Drupal::entityManager()->getStorage($this->get('parent_type')->value)->load($this->get('parent_id')->value); } diff --git a/src/ParagraphAccessControlHandler.php b/src/ParagraphAccessControlHandler.php index 3a82cee..28d7283 100644 --- a/src/ParagraphAccessControlHandler.php +++ b/src/ParagraphAccessControlHandler.php @@ -25,7 +25,7 @@ class ParagraphAccessControlHandler extends EntityAccessControlHandler { protected function checkAccess(EntityInterface $paragraph, $operation, AccountInterface $account) { // Allowed when the operation is not view or the status is true. /** @var \Drupal\paragraphs\Entity\Paragraph $paragraph */ - if (!$paragraph->isNew()) { + if (!$paragraph->isNew() && $paragraph->getParentEntity() != NULL) { $parent_access = $paragraph->getParentEntity()->access($operation, $account, TRUE); return AccessResult::allowedIf($operation != 'view' || $paragraph->status->value) ->andIf($parent_access); diff --git a/src/Tests/ParagraphsAccessTest.php b/src/Tests/ParagraphsAccessTest.php index c47090d..32bad42 100644 --- a/src/Tests/ParagraphsAccessTest.php +++ b/src/Tests/ParagraphsAccessTest.php @@ -13,7 +13,7 @@ use Drupal\user\RoleInterface; use Drupal\user\Entity\Role; /** - * Tests the security of paragraphs. + * Tests the access check of paragraphs. * * @group paragraphs */ @@ -99,10 +99,17 @@ class ParagraphsAccessTest extends WebTestBase { 'title[0][value]' => 'Security test node', 'files[field_paragraphs_demo_0_subform_field_images_demo_0][]' => $file_path, ); - $this->drupalPostForm(NULL, $edit, t('Save and publish')); + $image_url = file_create_url('private://privateImage.jpg'); + $this->drupalPostForm(NULL, $edit, t('Preview')); + $this->assertRaw($image_url, 'Image was found in preview'); + $this->clickLink(t('Back to content editing')); + $edit = [ + '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, 'Save and publish'); $node = $this->drupalGetNodeByTitle('Security test node'); - $image_url = file_create_url('private://privateImage.jpg'); $this->drupalGet('node/' . $node->id());