Skip to content

Make options hookable #321

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

BernhardBaumrock
Copy link
Contributor

Hey @ryancramerdesign I'm developing a new module with RockMigrations. To create an options field the migration looks like this:

<?php

namespace ProcessWire;

return [
  'type' => 'options',
  'inputfieldClass' => 'InputfieldRadios',
  'options' => [
    10 => 'a|automatic',
    20 => 'm|manually',
  ],
];

The problem is that I can not TRANSLATE option labels in this migration file for several reasons. This is not specific to options fields, but on all other fields I was able to translate labes notes and descriptions like this:

  protected function translateField(HookEvent $event): void
  {
    $f = $event->object;
    if (!$f instanceof Inputfield) return;
    $this->translate($f, 'label');
    $this->translate($f, 'notes');
    $this->translate($f, 'description');

    // get fieldtype
    $type = $f->hasFieldtype;

    // translate options
    if ($type instanceof FieldtypeOptions) {
      $options = $type->getOptions($f->hasField);
      foreach ($options as $option) {
        $option->title = $this->x($option->title);
      }
      // dumping options - titles are translated here!
      bd($options);
      $f->options = $options;
    }
  }

Unfortunately the field renders options with their original titles:

image

This missing hook has been a challenge for others (and myself) in the past: https://processwire.com/talk/topic/20321-add-custom-options-to-inputfieldselect-via-a-hook/

If there is a way to modify options of an options field already please let me know!

Otherwise thx for considering this PR :)

@BernhardBaumrock BernhardBaumrock changed the base branch from master to dev July 9, 2025 08:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy