/** * Builds the HTML for an individual block type. * If you don't pass in a block along with the type, then it'll render a base template to build real blocks from. * If you do pass in a block, then it's current field values will be rendered as well. * * @param Neo_BlockTypeModel $blockType * @param Neo_BlockModel|null $block * @param string $namespace * @param bool|false $static * @param $locale * @return array */ public function renderBlockTabs(Neo_BlockTypeModel $blockType, Neo_BlockModel $block = null, $namespace = '', $static = false, $locale = null) { $oldNamespace = craft()->templates->getNamespace(); $newNamespace = craft()->templates->namespaceInputName($namespace . '[__NEOBLOCK__][fields]', $oldNamespace); craft()->templates->setNamespace($newNamespace); $tabsHtml = []; $fieldLayout = $blockType->getFieldLayout(); $fieldLayoutTabs = $fieldLayout->getTabs(); if (!$block) { // Trick Craft into rendering fields of a block type with the correct locale (see below) $block = new Neo_BlockModel(); } if ($locale) { // Rendering the `_includes/fields` template doesn't take a `locale` parameter, even though individual // field templates do. If no locale is passed when rendering a field (which there won't be when using the // `fields` template) it defaults to the passed element's locale. The following takes advantage of this // by setting the locale on the block itself. In the event that only a block type is being rendered, the // above creates a dummy block to so the locale can be passed. $block->locale = $locale; } foreach ($fieldLayoutTabs as $fieldLayoutTab) { craft()->templates->startJsBuffer(); $tabHtml = ['name' => Craft::t($fieldLayoutTab->name), 'headHtml' => '', 'bodyHtml' => '', 'footHtml' => '', 'errors' => []]; $fieldLayoutFields = $fieldLayoutTab->getFields(); foreach ($fieldLayoutFields as $fieldLayoutField) { $field = $fieldLayoutField->getField(); $fieldType = $field->getFieldType(); if ($fieldType) { $fieldType->element = $block; $fieldType->setIsFresh($block == null); if ($block) { $fieldErrors = $block->getErrors($field->handle); if (!empty($fieldErrors)) { $tabHtml['errors'] = array_merge($tabHtml['errors'], $fieldErrors); } } } } $tabHtml['bodyHtml'] = craft()->templates->namespaceInputs(craft()->templates->render('_includes/fields', ['namespace' => null, 'element' => $block, 'fields' => $fieldLayoutFields, 'static' => $static])); foreach ($fieldLayoutFields as $fieldLayoutField) { $fieldType = $fieldLayoutField->getField()->getFieldType(); if ($fieldType) { $fieldType->setIsFresh(null); } } $tabHtml['footHtml'] = craft()->templates->clearJsBuffer(); $tabsHtml[] = $tabHtml; } craft()->templates->setNamespace($oldNamespace); return $tabsHtml; }