Skip to content

Commit

Permalink
Merge pull request #1982 from natanael89/EZP-27344_make_settings_bloc…
Browse files Browse the repository at this point in the history
…k_optional

EZP-27344: Make declaration of twig "settings" block optional for custom Field Types
  • Loading branch information
bdunogier committed Jun 20, 2017
2 parents de7bd00 + 6e8f001 commit ed6058c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
{{ ez_render_fielddefinition_settings( overrides ) }}
{{ ez_render_fielddefinition_settings( notdefault ) }}
{{ ez_render_fielddefinition_settings( withdata ) }}
{{ ez_render_fielddefinition_settings( notexisting ) }}
--DATA--
return array(
'nooverride' => $this->getFieldDefinition( 'eznooverride' ),
Expand All @@ -13,10 +14,12 @@ return array(
'withdata' => $this->getFieldDefinition(
'ezwithdata', 42,
array( 'frameworks' => array( 'YUI3', 'jQuery' ) )
)
),
'notexisting' => $this->getFieldDefinition( 'eznotexisting' )
)
--EXPECT--
default (no override)
override2
not default
42 ezwithdata YUI3, jQuery

32 changes: 19 additions & 13 deletions eZ/Publish/Core/MVC/Symfony/Templating/Twig/FieldBlockRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ public function renderContentFieldEdit(Field $field, $fieldTypeIdentifier, array
* @param array $params
* @param int $type Either self::VIEW or self::EDIT
*
* @throws MissingFieldBlockException If no template block can be found for $field
*
* @return string
*/
private function renderContentField(Field $field, $fieldTypeIdentifier, array $params, $type)
Expand All @@ -171,12 +173,15 @@ private function renderContentField(Field $field, $fieldTypeIdentifier, array $p
if (is_string($this->baseTemplate)) {
$this->baseTemplate = $this->twig->loadTemplate($this->baseTemplate);
}
$blockName = $this->getRenderFieldBlockName($fieldTypeIdentifier, $type);
$context = $this->twig->mergeGlobals($params);
$blocks = $this->getBlocksByField($fieldTypeIdentifier, $type, $localTemplate);

return $this->baseTemplate->renderBlock(
$this->getRenderFieldBlockName($fieldTypeIdentifier, $type),
$this->twig->mergeGlobals($params),
$this->getBlocksByField($fieldTypeIdentifier, $type, $localTemplate)
);
if (!$this->baseTemplate->hasBlock($blockName, $context, $blocks)) {
throw new MissingFieldBlockException("Cannot find '$blockName' template block.");
}

return $this->baseTemplate->renderBlock($blockName, $context, $blocks);
}

public function renderFieldDefinitionView(FieldDefinition $fieldDefinition, array $params = [])
Expand Down Expand Up @@ -206,12 +211,15 @@ private function renderFieldDefinition(FieldDefinition $fieldDefinition, array $
'fielddefinition' => $fieldDefinition,
'settings' => $fieldDefinition->getFieldSettings(),
];
$blockName = $this->getRenderFieldDefinitionBlockName($fieldDefinition->fieldTypeIdentifier, $type);
$context = $this->twig->mergeGlobals($params);
$blocks = $this->getBlocksByFieldDefinition($fieldDefinition, $type);

return $this->baseTemplate->renderBlock(
$this->getRenderFieldDefinitionBlockName($fieldDefinition->fieldTypeIdentifier, $type),
$this->twig->mergeGlobals($params),
$this->getBlocksByFieldDefinition($fieldDefinition, $type)
);
if (!$this->baseTemplate->hasBlock($blockName, $context, $blocks)) {
return '';
}

return $this->baseTemplate->renderBlock($blockName, $context, $blocks);
}

/**
Expand Down Expand Up @@ -290,8 +298,6 @@ private function getBlocksByFieldDefinition(FieldDefinition $definition, $type)
* @param string $name
* @param string $resourcesName
*
* @throws MissingFieldBlockException If no template block can be found for $field
*
* @return array
*/
private function getBlockByName($name, $resourcesName)
Expand All @@ -315,7 +321,7 @@ private function getBlockByName($name, $resourcesName)
}
}

throw new MissingFieldBlockException("Cannot find '$name' template block.");
return [];
}

/**
Expand Down

0 comments on commit ed6058c

Please sign in to comment.