Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
project
typed_entity
Commits
03a4d6d4
Commit
03a4d6d4
authored
May 08, 2020
by
e0ipso
Browse files
Issue
#3133714
: Create wrapReference method on WrappedEntityBase.php
parent
6dcba75f
Changes
6
Hide whitespace changes
Inline
Side-by-side
modules/typed_entity_example/src/TypedRepositories/ArticleRepository.php
View file @
03a4d6d4
...
...
@@ -2,11 +2,11 @@
namespace
Drupal\typed_entity_example\TypedRepositories
;
use
Drupal\Core\Entity\EntityTypeInterface
;
use
Drupal\Core\Entity\Query\ConditionInterface
;
use
Drupal\typed_entity
\
TypedRepositories\TypedEntityRepositoryBase
;
use
Drupal\typed_entity
\
WrappedEntityVariants\FieldValueVariantCondition
;
use
Drupal\typed_entity_example
\
WrappedEntities\BakingArticle
;
use
Symfony\Component\DependencyInjection\ContainerInterface
;
/**
* The repository for articles.
...
...
@@ -21,11 +21,15 @@ final class ArticleRepository extends TypedEntityRepositoryBase {
/**
* {@inheritdoc}
*/
public
function
__construct
(
ContainerInterface
$container
)
{
parent
::
__construct
(
$container
);
$this
->
variantConditions
=
[
new
FieldValueVariantCondition
(
static
::
FIELD_TAGS_NAME
,
24
,
BakingArticle
::
class
),
];
public
function
init
(
EntityTypeInterface
$entity_type
,
string
$bundle
,
string
$wrapper_class
):
void
{
parent
::
init
(
$entity_type
,
$bundle
,
$wrapper_class
);
$field_map
=
$this
->
container
->
get
(
'entity_field.manager'
)
->
getFieldMap
();
$has_field
=
$field_map
[
$entity_type
->
id
()][
static
::
FIELD_TAGS_NAME
][
'bundles'
][
$bundle
]
??
NULL
;
if
(
$has_field
)
{
$this
->
variantConditions
=
[
new
FieldValueVariantCondition
(
static
::
FIELD_TAGS_NAME
,
24
,
BakingArticle
::
class
),
];
}
}
/**
...
...
modules/typed_entity_example/src/WrappedEntities/Article.php
View file @
03a4d6d4
...
...
@@ -3,7 +3,9 @@
namespace
Drupal\typed_entity_example\WrappedEntities
;
use
Drupal\Core\Entity\EntityInterface
;
use
Drupal\Core\Entity\EntityViewBuilderInterface
;
use
Drupal\Core\Messenger\MessengerInterface
;
use
Drupal\typed_entity
\
RepositoryManager
;
use
Drupal\typed_entity
\
WrappedEntities\WrappedEntityBase
;
use
Drupal\typed_entity
\
WrappedEntities\WrappedEntityInterface
;
use
Symfony\Component\DependencyInjection\ContainerInterface
;
...
...
@@ -25,11 +27,15 @@ final class Article extends WrappedEntityBase {
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to wrap.
* @param \Drupal\typed_entity\RepositoryManager $repository_manager
* The repository manager.
* @param \Drupal\Core\Entity\EntityViewBuilderInterface $entity_view_builder
* The view builder.
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* The messenger service.
*/
public
function
__construct
(
EntityInterface
$entity
,
MessengerInterface
$messenger
)
{
parent
::
__construct
(
$entity
);
public
function
__construct
(
EntityInterface
$entity
,
RepositoryManager
$repository_manager
,
EntityViewBuilderInterface
$entity_view_builder
,
MessengerInterface
$messenger
)
{
parent
::
__construct
(
$entity
,
$repository_manager
,
$entity_view_builder
);
$this
->
messenger
=
$messenger
;
}
...
...
@@ -37,8 +43,13 @@ final class Article extends WrappedEntityBase {
* {@inheritdoc}
*/
public
static
function
create
(
ContainerInterface
$container
,
EntityInterface
$entity
)
{
$entity_view_builder
=
$container
->
get
(
'entity_type.manager'
)
->
getViewBuilder
(
$entity
->
getEntityTypeId
());
$repository_manager
=
$container
->
get
(
RepositoryManager
::
class
);
return
new
static
(
$entity
,
$repository_manager
,
$entity_view_builder
,
$container
->
get
(
'messenger'
)
);
}
...
...
src/WrappedEntities/WrappedEntityBase.php
View file @
03a4d6d4
...
...
@@ -53,6 +53,13 @@ abstract class WrappedEntityBase implements WrappedEntityInterface, RenderableIn
*/
protected
$viewMode
=
'default'
;
/**
* The entity.
*
* @var \Drupal\Core\Entity\EntityInterface
*/
protected
$entity
;
/**
* The view builder.
*
...
...
@@ -61,22 +68,25 @@ abstract class WrappedEntityBase implements WrappedEntityInterface, RenderableIn
protected
$viewBuilder
;
/**
* The
entity
.
* The
repository manager
.
*
* @var \Drupal\
Core\Entity\EntityInterface
* @var \Drupal\
typed_entity\RepositoryManager
*/
protected
$
entity
;
protected
$
repositoryManager
;
/**
* WrappedEntityBase constructor.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to wrap.
* @param \Drupal\typed_entity\RepositoryManager $repository_manager
* The repository manager.
* @param \Drupal\Core\Entity\EntityViewBuilderInterface $entity_view_builder
* The view builder.
*/
public
function
__construct
(
EntityInterface
$entity
,
EntityViewBuilderInterface
$entity_view_builder
)
{
public
function
__construct
(
EntityInterface
$entity
,
RepositoryManager
$repository_manager
,
EntityViewBuilderInterface
$entity_view_builder
)
{
$this
->
entity
=
$entity
;
$this
->
repositoryManager
=
$repository_manager
;
$this
->
viewBuilder
=
$entity_view_builder
;
}
...
...
@@ -86,7 +96,8 @@ abstract class WrappedEntityBase implements WrappedEntityInterface, RenderableIn
public
static
function
create
(
ContainerInterface
$container
,
EntityInterface
$entity
)
{
$entity_view_builder
=
$container
->
get
(
'entity_type.manager'
)
->
getViewBuilder
(
$entity
->
getEntityTypeId
());
return
new
static
(
$entity
,
$entity_view_builder
);
$repository_manager
=
$container
->
get
(
RepositoryManager
::
class
);
return
new
static
(
$entity
,
$repository_manager
,
$entity_view_builder
);
}
/**
...
...
@@ -106,21 +117,13 @@ abstract class WrappedEntityBase implements WrappedEntityInterface, RenderableIn
/**
* {@inheritdoc}
*
* @SuppressWarnings(PHPMD.StaticAccess)
*/
public
function
owner
():
?WrappedEntityInterface
{
$owner_key
=
$this
->
getEntity
()
->
getEntityType
()
->
getKey
(
'owner'
);
if
(
!
$owner_key
)
{
return
NULL
;
}
$owner
=
$this
->
getEntity
()
->
{
$owner_key
}
->
entity
;
if
(
!
$owner
instanceof
EntityInterface
)
{
return
NULL
;
}
$manager
=
\
Drupal
::
service
(
RepositoryManager
::
class
);
assert
(
$manager
instanceof
RepositoryManager
);
return
$manager
->
wrap
(
$owner
);
return
$this
->
wrapReference
(
$owner_key
);
}
/**
...
...
@@ -140,4 +143,47 @@ abstract class WrappedEntityBase implements WrappedEntityInterface, RenderableIn
return
$this
->
viewBuilder
->
view
(
$this
->
getEntity
(),
$this
->
viewMode
);
}
/**
* Wraps all the entities referenced by the field name.
*
* @param string $field_name
* The name of the entity reference field.
*
* @return \Drupal\typed_entity\WrappedEntities\WrappedEntityInterface[]
* The wrapped referenced entities.
*
* @throws \Drupal\typed_entity\InvalidValueException
*/
public
function
wrapReferences
(
string
$field_name
):
array
{
$references
=
[];
foreach
(
$this
->
getEntity
()
->
{
$field_name
}
as
$item
)
{
$target_entity
=
$item
->
entity
;
if
(
!
$target_entity
instanceof
EntityInterface
)
{
continue
;
}
$references
[]
=
$this
->
repositoryManager
->
wrap
(
$target_entity
);
}
return
$references
;
}
/**
* Wraps the first entity referenced by the field name.
*
* @param string $field_name
* The name of the entity reference field.
*
* @return \Drupal\typed_entity\WrappedEntities\WrappedEntityInterface
* The wrapped referenced entity.
*
* @throws \Drupal\typed_entity\InvalidValueException
*/
public
function
wrapReference
(
string
$field_name
):
?WrappedEntityInterface
{
$target_entity
=
$this
->
getEntity
()
->
{
$field_name
}
->
entity
;
if
(
!
$target_entity
instanceof
EntityInterface
)
{
return
NULL
;
}
return
$this
->
repositoryManager
->
wrap
(
$target_entity
);
}
}
tests/modules/typed_entity_test/src/TypedRepositories/ArticleRepository.php
View file @
03a4d6d4
...
...
@@ -2,10 +2,10 @@
namespace
Drupal\typed_entity_test\TypedRepositories
;
use
Drupal\Core\Entity\EntityTypeInterface
;
use
Drupal\typed_entity
\
TypedRepositories\TypedEntityRepositoryBase
;
use
Drupal\typed_entity
\
WrappedEntityVariants\FieldValueVariantCondition
;
use
Drupal\typed_entity_test
\
WrappedEntities\NewsArticle
;
use
Symfony\Component\DependencyInjection\ContainerInterface
;
/**
* The repository for articles.
...
...
@@ -15,10 +15,10 @@ class ArticleRepository extends TypedEntityRepositoryBase {
/**
* {@inheritdoc}
*/
public
function
__construct
(
ContainerInterface
$container
)
{
parent
::
__construct
(
$container
);
public
function
init
(
EntityTypeInterface
$entity_type
,
string
$bundle
,
string
$wrapper_class
):
void
{
parent
::
init
(
$entity_type
,
$bundle
,
$wrapper_class
);
$this
->
variantConditions
=
[
new
FieldValueVariantCondition
(
'field_type'
,
'News'
,
NewsArticle
::
class
),
new
FieldValueVariantCondition
(
'field_
node_
type'
,
'News'
,
NewsArticle
::
class
),
];
}
...
...
tests/src/Kernel/KernelTestBase.php
View file @
03a4d6d4
...
...
@@ -53,7 +53,7 @@ abstract class KernelTestBase extends BaseTestsKernelTestBase {
$node_type
->
save
();
$field_storage
=
FieldStorageConfig
::
create
([
'field_name'
=>
'field_type'
,
'field_name'
=>
'field_
node_
type'
,
'entity_type'
=>
'node'
,
'type'
=>
'string'
,
]);
...
...
tests/src/Kernel/TypedEntityRepositoryTest.php
View file @
03a4d6d4
...
...
@@ -42,7 +42,7 @@ class TypedEntityRepositoryTest extends KernelTestBase {
$article_wrapper
=
$repository
->
wrap
(
$article
);
$this
->
assertInstanceOf
(
Article
::
class
,
$article_wrapper
);
$article
->
field_type
->
value
=
'News'
;
$article
->
field_
node_
type
->
value
=
'News'
;
$article
->
save
();
$article_wrapper
=
$repository
->
wrap
(
$article
);
$this
->
assertInstanceOf
(
NewsArticle
::
class
,
$article_wrapper
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment