The new way to render an attribute form in a single page

Permalink
I have a single page in backend which display form elements based on the page attribute:
$attribute_key = 'product_description';
$akt = CollectionAttributeKey::getByHandle($attribute_key);
echo $akt->render('form', 'Default Value', true);

As I just checked the render method is labeled 'deprecated' in
\concrete\src\Entity\Attribute\Key\Key.php: line 262
Is there a better way to do such a thing in the newer versions of concrete5?

 
mnakalay replied on at Permalink Best Answer Reply
mnakalay
Hello,
it did get a little more complex which is unfortunate.

Here's how to do it
use Concrete\Core\Attribute\Context\FrontendFormContext;
use Concrete\Core\Attribute\Form\Renderer;
use Concrete\Core\Attribute\Category\CategoryService;
use Concrete\Core\Support\Facade\Application;
use Concrete\Core\Page\Page;
$app = Application::getFacadeApplication();
$service = $app->make(CategoryService::class);
$categoryEntity = $service->getByHandle('collection');
$category = $categoryEntity->getController();
$attribute_key = 'product_description';
$akt = $category->getByHandle($attribute_key);
$renderer = new Renderer(new FrontendFormContext(), Page::getCurrentPage());
$renderer->buildView($akt)->render();

This will give you the input with the attribute name as label and will populate it with any existing value
Herkool replied on at Permalink Reply
Thank you.
While on this, is it possible to change the layout? What I would rather do is in some cases using inline form formatting or adding a few lines of guideline.
mnakalay replied on at Permalink Reply
mnakalay
For that you'll be entering a whole world of pain. There's a whole bunch of tutorials in the Dev docs to show you how to customize Express forms. The principle is the same here. You have to customize the context. If you noticed I'm using a context class in the code to generate the attribute input.

Here's one tutorial that will put you in the right directionhttps://documentation.concrete5.org/developers/express/express-forms...

If your needs are mostly design-related, I strongly suggest you try to d what you want with CSS only without trying to modify the markup by modifying the whole context thing.