GithubHelp home page GithubHelp logo

apostrophecms / apostrophe-personas Goto Github PK

View Code? Open in Web Editor NEW
3.0 15.0 2.0 130 KB

Specialize the content of each page of an Apostrophe site based on the user's primary affiliation (employee versus employer, truck vs. car, etc.)

License: MIT License

JavaScript 95.79% HTML 4.21%

apostrophe-personas's Introduction

apostrophe-personas

This module helps you specialize the content of each page of an Apostrophe site based on the user's primary affiliation. Here are good examples:

  • Employee versus employer
  • Trucks versus cars
  • "Prosumer" versus low-end gear

Consider the employee versus employer example. Every widget on the site can be designated as universal, employee-oriented, or employer-oriented. By default, users see all of these, as the site doesn't yet know which is relevant.

But as soon as the user takes action to indicate their employee status, a prefix is added to the URL and they see only universal widgets and employee widgets, unless they decide to switch back.

Also, pages can be designated as oriented toward one persona or the other. This can be used to avoid showing them as navigation options to uninterested parties.

Page URLs are always prefixed based on the active persona, which improves bookmarking and social sharing outcomes as well as allowing search engines to index them with different content subsets.

Example

// in app.js
modules: {
  'apostrophe-personas': {
    personas: [
      {
        name: 'employee',
        label: 'Employee',
        prefix: '/employee'
      },
      {
        name: 'employer',
        label: 'Employer',
        prefix: '/employer'
      }
    ]
  }
}

Personas and workflow

This module can optionally be used together with the apostrophe-workflow module. If so, the workflow module must be configured first.

When workflow is present, any URL prefix for the workflow locale comes first, and the persona prefixes themselves should be localized. Here is an example.

// in app.js
modules: {
  'apostrophe-workflow': {
    locales: [
      {
        name: 'en',
        label: 'English'
      },
      {
        name: 'fr',
        label: 'French'
      }
    ],
    prefixes: {
      'en': '/en',
      'fr': '/fr'
    }
  },
  'apostrophe-personas': {
    personas: [
      {
        name: 'employee',
        label: 'Employee',
        prefixes: {
          'en': '/employee',
          'fr': '/employé'
        }
      },
      {
        name: 'employer',
        label: 'Employer',
        prefixes: {
          'en': '/employer',
          'fr': '/employeur'
        }
      }
    ]
  }
}

The resulting URLs look like:

/en/employer/about /fr/employeur/about

Of course the admin can edit the /about part of the French slug to suit the language. That's an apostrophe-workflow feature.

Both of these URLs reference the same persona, but in different locales.

Since a single doc object serves all personas, the persona prefix does not become part of the slug in the database. The URL is rewritten dynamically as needed.

If you do not specify the prefixes option, or leave out locales, a warning is printed, as an untranslated prefix is usually not what you really want.

Constructing links to a specific persona

Normally, links generated to pages or pieces on the site will have the same persona prefix that is already present in the address bar for the current page.

However it is possible to create link widgets that link to a user-specified persona.

To do that, just create your own link widget as you normally do, and include a field called linkToPersona.

You may specify an empty choices array, in which case apostrophe-personas will fill in the choices for you. Or, you may specify the exact choices you want to offer, in which case they must exactly match the persona names in your apostrophe-personas configuration.

Make sure the widget also has a joinByOne or joinByArray field whose withType property is set to apostrophe-page, or to a piece type which has corresponding pieces-pages on the site (or otherwise generates a valid _url property when loaded). Note that it must be at the same level of the schema.

When you do so, apostrophe-personas will automatically detect this situation and correct the generated links to match the specified persona.

Note that if you are completely overriding the load method of your widget without calling the original version of the method, this mechanism will not work automatically. However it is also possible to call the addPrefix(req, personaName, url) method of the apostrophe-personas module to retrieve a version of any URL that has been corrected for the specified persona.

Forcing persona selection

If the disableEmptyUniversal option is set to true, this option forces a persona to be selected at all times. When a universal document is requested with no persona prefix, the user is redirected to the persona found in options.defaultPersonaByLocale (an object with values for each locale), or to options.defaultPersona, or finally to the first configured persona.

Because this option will cause a redirect to add the prefix for the persona, code for custom routes may occasionally respond in surprising ways. You can disable the effect of this option for such URLs via the neverForcePersona option, which can be set to an array of URLs. It is concatenated with minimumNeverForcePersona, which defaults to:

minimumNeverForcePersona: [ '/login-totp', '/setup-totp', '/confirm-totp' ]

Detecting the persona in templates

In your templates, data.persona indicates the current persona.

data.isPersonaUniversalContext will be true if the current document has a universal persona (useful for persona switcher display in authoring).

data.personaSwitched indicates that the persona just changed.

apostrophe-personas's People

Contributors

boutell avatar daklik avatar grdunn avatar starsinmypockets avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

apostrophe-personas's Issues

Persona-specific content appears in no-persona context

Between v3.0.0 and v3.0.1, it appears the functionality changed for the "root" context, where there is no persona. When viewing a page without a persona, widgets of all personas appear. I was surprised to see this published a patch release, because it is a major functional change that impacted our site. We had been treating the "root" context as a default and allowing specific content areas to be overridden by the specific persona. For instance, on a landing page, we have a logo area that varies from one persona to another, but without any persona, users would see a default logo. With this patch, all of the logos appear. This reduces the versatility of this feature, because now there is no way to give a content area some sort of fallback default, and every user must be routed into a persona.

Personas module warns about prefixes not defined when I'm not even using locales

Every time I start my Apostrophe server, I get several warnings like this:

Warning: workflow module is in use and the prefixes option for the acme persona has no setting for the default locale, falling back to /acme which will not be translated

I get one of these warnings for every persona I have defined. I checked the source code, and it's caused by no prefixes option being defined. However, it later checks for the workflow locales being defined, and I don't have any, because I am setting up a one-locale solution (per instructions on https://github.com/apostrophecms/apostrophe-workflow )

Do I need to specify a single default locale for personas? This seems like overkill, and I want to avoid potential impact to my workflow configuration.

Please advise. Is this something I need to account for? Or can this be fixed in the personas module? I'd like to understand better why apostrophe-personas assumes that locales will be defined in workflow.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.