3333use ContaoCommunityAlliance \DcGeneral \Contao \View \Contao2BackendView \Event \ModelToLabelEvent ;
3434use ContaoCommunityAlliance \DcGeneral \Contao \View \Contao2BackendView \Event \ManipulateWidgetEvent ;
3535use ContaoCommunityAlliance \DcGeneral \Controller \ModelCollector ;
36+ use ContaoCommunityAlliance \DcGeneral \Controller \RelationshipManager ;
3637use ContaoCommunityAlliance \DcGeneral \Data \ModelId ;
38+ use ContaoCommunityAlliance \DcGeneral \Data \ModelInterface ;
3739use ContaoCommunityAlliance \DcGeneral \EnvironmentInterface ;
3840use ContaoCommunityAlliance \DcGeneral \Factory \DcGeneralFactory ;
3941use ContaoCommunityAlliance \Translator \TranslatorInterface ;
@@ -210,9 +212,6 @@ public function handleModelToLabel(ModelToLabelEvent $event)
210212 * @param GetPasteButtonEvent $event The event.
211213 *
212214 * @return void
213- *
214- * @SuppressWarnings(PHPMD.Superglobals)
215- * @SuppressWarnings(PHPMD.CamelCaseVariableName)
216215 */
217216 public function generatePasteButton (GetPasteButtonEvent $ event )
218217 {
@@ -234,18 +233,19 @@ public function generatePasteButton(GetPasteButtonEvent $event)
234233 return ;
235234 }
236235
237- $ flags = $ GLOBALS ['METAMODELS ' ]['inputscreen_conditions ' ][$ model ->getProperty ('type ' )];
238236 // If setting does not support children, omit them.
239- if ($ model ->getId () &&
240- (!$ flags ['nestingAllowed ' ])
241- ) {
237+ $ collector = new ModelCollector ($ environment );
238+ if ($ model ->getId () && !$ this ->acceptsAnotherChild ($ model , $ collector )) {
242239 $ event ->setPasteIntoDisabled (true );
243- return ;
244240 }
245241
246- $ collector = new ModelCollector ($ environment );
247- if (isset ($ flags ['maxChildren ' ]) && count ($ collector ->collectChildrenOf ($ model )) > $ flags ['maxChildren ' ]) {
248- $ event ->setPasteIntoDisabled (true );
242+ $ definition = $ environment ->getDataDefinition ();
243+ $ mode = $ definition ->getBasicDefinition ()->getMode ();
244+ $ relationships = new RelationshipManager ($ definition ->getModelRelationshipDefinition (), $ mode );
245+ if (!$ relationships ->isRoot ($ model )
246+ && ($ parent = $ collector ->searchParentOf ($ model ))
247+ && !$ this ->acceptsAnotherChild ($ parent , $ collector )) {
248+ $ event ->setPasteAfterDisabled (true );
249249 }
250250 }
251251
@@ -488,6 +488,33 @@ public function encodeValueValue(EncodePropertyValueFromWidgetEvent $event)
488488 }
489489 }
490490
491+ /**
492+ * Test if a model accepts another child.
493+ *
494+ * @param ModelInterface $model The model that shall be checked.
495+ * @param ModelCollector $collector The collector to use.
496+ *
497+ * @return bool
498+ *
499+ * @SuppressWarnings(PHPMD.Superglobals)
500+ * @SuppressWarnings(PHPMD.CamelCaseVariableName)
501+ */
502+ private function acceptsAnotherChild (ModelInterface $ model , ModelCollector $ collector )
503+ {
504+ $ conditionType = $ model ->getProperty ('type ' );
505+ $ flags = $ GLOBALS ['METAMODELS ' ]['inputscreen_conditions ' ][$ conditionType ];
506+ if (!$ flags ['nestingAllowed ' ]) {
507+ return false ;
508+ }
509+
510+ if (isset ($ flags ['maxChildren ' ])
511+ && count ($ collector ->collectDirectChildrenOf ($ model )) > $ flags ['maxChildren ' ]) {
512+ return false ;
513+ }
514+
515+ return true ;
516+ }
517+
491518 /**
492519 * Obtain the values of a property within a dc-general instance.
493520 *
0 commit comments