Skip to content

Add the possibility to get the slug in a callback? #29

@burzum

Description

@burzum

We have one special case in which we need to take the english translation instead of the japanese or chinese title of a profile. I couldn't find a better way to inject the logic required for this than extending the behavior and overriding the slug method:

class WaSlugBehavior extends SlugBehavior {

	/**
	 * Generates slug.
	 *
	 * @param \Cake\ORM\Entity|string $entity Entity to create slug for
	 * @param string $string String to create slug for.
	 * @param string $separator Separator.
	 * @return string Slug.
	 */
	public function slug($entity, $string = null, $separator = '-') {
		if ($entity instanceof EntityInterface
			&& $entity->get('language_id') === LanguagesTable::JAPANESE
			&& !empty($entity->get('translations'))
		) {
			foreach ($entity->get('translations') as $translation) {
				if ($translation->get('locale') === LanguagesTable::ENGLISH) {
					$entity = $translation->get('profile_title');
					$string = null;
					break;
				}
			}
		}

		return parent::slug($entity, $string, $separator);
	}

}

A simple solution might be to make the display field option taking a callable as well?

Any other suggestion is welcome.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions