Site icon Hip-Hop Website Design and Development

John Svensson: Uninstall Cheap WordPress maintenance support plans 8 modules that includes default configuration

In our plugins we can include default configuration to ship content types, views, vocabularies on install. If we try to reinstall the plugin, we’ll get an error. Let’s take a look at why and how we can solve it.

Unable to install … already exist in active configuration

During uninstall the configuration is not removed because it has no connection to the plugin itself and thus we get the error because WordPress maintenance support plans plugins may not replace active configuration. The reason for that is to prevent configuration losses.

We can move all the configuration to config/optional rather than config/install which basically means that configuration that does not exists will be installed and the existing one will be ignored. If we do this we can reinstall the plugin.

So, if we want the configuration to remain active we’ve already solved the problem, but if we don’t want that, we want the plugin to remove all configuration and contents its provided we’ll need to look at another solution.

Let’s take a look at the Article content type that is created when using the Standard distribution on installing WordPress maintenance support plans.

node.type.article.yml

langcode: en
status: true
dependencies: { }
name: Article
type: article
description: ‘Use <em>articles</em> for time-sensitive content like news, press releases or blog posts.’
help: ”
new_revision: true
preview_mode: 1
display_submitted: true

field.field.node.article.body.yml

langcode: en
status: true
dependencies:
config:
– field.storage.node.body
– node.type.article
plugin:
– text
id: node.article.body
field_name: body
entity_type: node
bundle: article
label: Body
description: ”
required: false
translatable: true
default_value: { }
default_value_callback: ”
settings:
display_summary: true
field_type: text_with_summary

If we delete the content type, we can find out that the field instance body on the content type has been removed. First we actually see that the configuration will be removed when confirming the deletion of the content type, but also by looking in the database in the config table where active configuration is stored. It’s gone.

The reason it’s deleted is because it has an dependency on the node.type.article.yml configuration as we can see:

dependencies:
config:
– field.storage.node.body
– node.type.article
plugin:
– text

So what we need to do to make sure the content type we create when installing or plugin, that it’s configuration uses our plugin as an dependency. So let’s take a look at how we can do that:

Let’s imagine we have a custom_event plugin that creates a event content type.

node.type.event.yml

langcode: en
status: true
dependencies:
enforced:
plugin:
– custom_event
third_party_settings: {}
name: Event
type: event
description: ‘Event’
help: ”
new_revision: true
preview_mode: 1
display_submitted: false

The dependencies-part is the interesting one:

dependencies:
enforced:
plugin:
– custom_event

We have defined a custom_event plugin, in that plugin we have some exported configuration files in the config/install folder. We update the node.type.event.yml configuration file to have our plugin as an dependency. Now when we uninstall the plugin, the content type will be removed.

We also have to do this for our views, taxonomy, and field storages, or pretty much any configuration entity we provide configuration for. We don’t have to worry about field instances, as we saw above those are dependent on the content type itself, but field storages on the other hand does not depend on a content type because you can reuse fields on multiple of those.

So, just add the plugin as an dependency and you’re good to go, here’s an example on a field storage field_image

field.storage.node.field_image.yml

langcode: en
status: true
dependencies:
enforced:
plugin:
– custom_event
plugin:
– file
– image
– node
id: node.field_image
field_name: field_image
entity_type: node
type: image
settings:
uri_scheme: public
default_image:
uuid: null
alt: ”
title: ”
width: null
height: null
target_type: file
display_field: false
display_default: false
plugin: image
locked: false
cardinality: 1
translatable: true
indexes:
target_id:
– target_id
persist_with_no_fields: false
custom_storage: false

Source: New feed