Jump to content

Composing title field using other fields


Manaus
 Share

Recommended Posts

I'd like to have a title field "First name second name" which is automatically composed by a "First name" and a "Second name" fields.

For example, if I have a page with John Ford as first name and second name respectively, I'd like to have the title as "John Ford" (without quotes).

Is it possible?

Thanks

Link to comment
Share on other sites

Hi, 

You could hook after "InputfieldText::processInput". That way you can also check for the uniquness of the name/title if you need to, and add your error messages as needed.

Here is a similar hook I use in a project:

<?php namespace ProcessWire;
wire()->addHookAfter("InputfieldText::processInput", function ($event) {
    /* @var $page AddonPage */
    $field = $event->object;
  
    if ($field->name == "suffix") {
        $page = modules()->ProcessPageEdit->getPage();
        if ($page->template != "addon") return;

        //The final title and name is based on the fields: addon_addon + suffix (both are text fields)
        $suffix = $field->value;  
        $suffixFormatted = " (" . $field->value . ")";

        $addon = session()->get('addon_addon');
        session()->remove('addon_addon');

        $titleNew = empty($suffix) ? $addon : $addon . $suffixFormatted;

        $nameNew = wire()->sanitizer->pageName($titleNew, true);
        $uniqueName = pages()->names()->uniquePageName($nameNew, $page);

        $titleSelector = wire()->sanitizer->selectorValue($titleNew);
        $existingPage = wire()->pages->get("template=addon, title={$titleSelector}");
        $hasErrors = empty($addon);

        if ($existingPage->id == $page->id && !$hasErrors) {
            //bd("Page name has not changed, he have no errors, so no need to proceed...");
            return;
        }

        if ($existingPage->id != 0 || $hasErrors) {
            //bd("We had errors, we need to clean things up...");
            $titleUntitled = "addon-" . date("YmdHis");
            $nameUntitled = wire()->sanitizer->pageName($titleUntitled, true);
            $uniqueNameUntitled = pages()->names()->uniquePageName($nameUntitled, $page);
            $page->name = $uniqueNameUntitled;
            $page->title = $titleUntitled;
            $page->addon = $addon;
            if ($hasErrors) {
                session()->error("Errors listed below must be fixed for this add-on to be saved properly!");
            } else {
                session()->error("Title is already in use by an existing one (ID '{$existingPage->id}'). Either change the Label and/or add a unique suffix!");
            }
        } else {
            //bd("No errors, We can save provided values...");
            $page->title = $titleNew;
            $page->name = $uniqueName;
        }
    }
});

Note that the admin "new page creation process" skips the step asking for the title in the first place, so I had to handle the case of blank values as well when the user abandons the complete process of creating a page with the required text fields being filled in and saved.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...