npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

datocms-plugin-use-link-as-defaults

v0.0.6

Published

This plugin provides the developer with an easy way to configure fields that a CMS user can overwrite manually when linking an entity.

Downloads

2

Readme

DatoCMS Plugin: Use Link as Defaults

This plugin provides the developer with an easy way to configure fields that a CMS user can overwrite manually when linking an entity.

The CMS user selects an instance of a model and fields are being prefilled with data coming from the selected entity.

You can add this field addon only to single link fields.

Preview

Preview

What this plugin can do for you

Let's imagine the following scenario: You have a website where you want to display certain models on your front page. DatoCMS solves this for you by linking different references. Unfortunately, these linked entities can not be customized for this one specific link to the homepage. Here is where this plugin will help you out. It lets you link whatever model you want and offers you the option to provide a mapping function that will then map fields from the linked element and use them as default values on a custom teaser.

So let's image, an editor wants to feature an article and a product on your frontpage. Let's further assume we have two models defined as follow:

| Article Model | Product Model | | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- | | Model Demo Article | Model Demo Product |

Let's create four instances of these models:

| | Example 1 | Example 2 | | ------- | ---------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------- | | Article | Article 1 | Article 2 | | Product | Product 1 | Product 2 |

Now we will configure a custom teaser with these settings:

Custom Teaser Model

Where reference is of type single link and has both demo models selected. Within the edit section of the reference field, we set up the config. Go to presentation tab and select "Insert Addon" and select "Use Link as Defaults". A new field will be added called configuration which expects a valid JSON config.

Config for Custom Teaser

{
  "fields": [ // list all fields of the current model
    "title",
    "additional_text",
    "image",
    "category"
  ],
  "assign": {
    "demo_article_model": { // selected models
      "title": "title", // map source field to target field
      "additional_text": "summary",
      "image": "image",
      "category": "category"
    },
    "demo_product_model": {
      "title": "name",
      "additional_text": "information",
      "image": "product_images[0]", // use lodash like selectors
      "category": "brand"
    }
  }
}

That's it.

Demo

Use different models as reference and map fields. Demo 1

Overwrite specific fields manually and do not reset them. Empty them for resetting. Demo 2

Plugin Configuration

{
  "global": [
    {
      "id": "developmentMode",
      "label": "Development Mode",
      "type": "boolean",
      "default": false,
      "hint": "Shows debug messages in console"
    },
    {
      "id": "apiToken",
      "label": "Read-Only API Token",
      "type": "string",
      "required": true,
      "default": "",
      "hint": "The plugin needs to fetch data in order to display it."
    },
    {
      "id": "textNothingSet",
      "label": "Text for nothing set state",
      "type": "string",
      "hint": "Default: Select a reference above"
    },
    {
      "id": "textClearButton",
      "label": "Text for Clear Button",
      "type": "string",
      "hint": "Default: Clear all fields"
    },
    {
      "id": "textResetButton",
      "label": "Text for Reset Button",
      "type": "string",
      "hint": "Default: Reset empty fields"
    }
  ],
  "instance": [
    {
      "id": "config",
      "label": "Configuration",
      "type": "json",
      "required": true
    }
  ]
}

Instance Configuration

Structure:

{
  "fields": ["targetFieldName"]
  "assign": {
    "model1": {
      "targetFieldName": "sourceFieldName1",
    },
    "model2": {
      "targetFieldName": "sourceFieldName2",
    }
  }
}

Examples:

{
  "fields": [
    "title",
    "additional_text",
    "image",
    "category"
  ],
  "assign": {
    "product": {
      "title": "brand.name",
      "additional_text": "name",
      "image": "image",
      "category": "categories[0].name" // take the first categorie's name
    },
    "category_page": {
      "title": "title",
      "additional_text": "introduction",
      "image": "products[1].image", // take the second image from linked field products
      "category": "products[0].categories[0].name" // linked field
    }
  }
}
{
  "fields": [ // list all fields of the current model
    "title",
    "additional_text",
    "image",
    "category"
  ],
  "assign": {
    "demo_article_model": { // selected models
      "title": "title", // map source field to target field
      "additional_text": "summary",
      "image": "image",
      "category": "category"
    },
    "demo_product_model": {
      "title": "name",
      "additional_text": "information",
      "image": "product_images[0]", // use lodash like selectors
      "category": "brand"
    }
  }
}

Development

Install all the project dependencies with:

yarn install

Start the local development server with:

yarn start

The plugin will be served from http://localhost:5500/. Insert this URL as the plugin Entry point URL.