65 $this->contextFactory = $context_factory;
71 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
73 $container->get(
'paragraphs_editor.field_value.manager'),
74 $container->get(
'entity_type.manager')->getStorage(
'paragraph'),
75 $container->get(
'paragraphs_editor.command.context_factory')
82 public function analyze(SemanticDataInterface $data) {
83 if ($data->is($this->getSelector(
'widget'))) {
86 elseif ($data->is($this->getSelector(
'field'))) {
105 $uuid = $this->
getAttribute($data->node(),
'widget',
'<uuid>');
114 'context_id' => $this->
getAttribute($data->node(),
'widget',
'<context>'),
118 'context_id' => $data->get(
'field.context_id'),
122 'items' => $data->get(
'field.items'),
129 while (!empty($attempts)) {
130 $attempt = array_shift($attempts);
132 if ($attempt[
'type'] ==
'context') {
133 $context = $this->contextFactory->get($attempt[
'context_id']);
134 if (!empty($context)) {
135 $edit_buffer = $context->getEditBuffer();
136 $item = $edit_buffer->getItem($uuid);
138 return $data->tag(
'paragraph', [
139 'entity' => $item->getEntity(),
140 'context_id' => $attempt[
'context_id'],
145 elseif ($attempt[
'type'] ==
'items' && $attempt[
'items']) {
146 foreach ($this->fieldValueManager->getReferencedEntities($attempt[
'items']) as $candidate) {
147 if ($candidate->uuid() == $uuid) {
148 return $data->tag(
'paragraph', [
149 'entity' => $candidate,
154 elseif ($attempt[
'type'] ==
'storage') {
155 $matches = $this->storage->loadByProperties([
158 if (!empty($matches)) {
159 return $data->tag(
'paragraph', [
160 'entity' => reset($matches),
165 catch (\Exception $e) {
166 throw new DomProcessorError(
"Unkown load type.");
170 throw new DomProcessorError(
"Could not load entity.");
184 $field_name = $this->
getAttribute($data->node(),
'field',
'<name>');
185 $paragraph = $data->get(
'paragraph.entity');
186 if (!$field_name || !$paragraph || !isset($paragraph->{$field_name})) {
187 throw new DomProcessorError(
"Could not access field on entity.");
190 $items = $paragraph->{$field_name};
191 $field_definition = $items->getFieldDefinition();
192 if (!$this->fieldValueManager->isParagraphsField($field_definition)) {
193 throw new DomProcessorError(
"Attempted to access non-paragraphs field.");
196 if ($this->fieldValueManager->isParagraphsEditorField($field_definition)) {
197 $field_value_wrapper = $this->fieldValueManager->wrapItems($items);
198 $field_value_wrapper->setReferencedEntities([]);
199 $context_id = $this->
getAttribute($data->node(),
'field',
'<context>');
203 $field_value_wrapper = NULL;
206 return $data->tag(
'field', [
208 'context_id' => $context_id,
209 'wrapper' => $field_value_wrapper,
analyze(SemanticDataInterface $data)
__construct(FieldValueManagerInterface $field_value_manager, EntityStorageInterface $storage, CommandContextFactoryInterface $context_factory)
static create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition)
trait ParagraphsEditorElementTrait
analyzeField(SemanticDataInterface $data)
analyzeWidget(SemanticDataInterface $data)
initializeParagraphsEditorElementTrait(FieldValueManagerInterface $field_value_manager)
getAttribute(\DOMNode $node, $element_name, $attribute_name)