logo

パレット - カラフルに🎨

Palette — ビジュアルページビルダー、デザインの専門知識は不要です。

ライブデモ パレットをダウンロード

Scroll

カスタムフィールドをコンテンツタイプにプログラミングする

17/05/2020, by maria

カスタムモジュールからコンテンツタイプをエクスポートするとき、このコンテンツタイプに属するフィールドも追加したい場合があります。フィールドを自動的に作成できるため、余分なフィールドを残さず、追加し忘れないことを保証しながら、複数のサイトで削除および再インストールできます。これらのフィールドをコードベースに追加するには2つの方法があり、それらを確認します。

前提条件

  • Drupal 8.0.x がインストールされていること。
  • カスタムモジュールを持っていること(この例で使用されるモジュール名は foobar です)。
  • モジュール内にカスタムコンテンツタイプを持っていること(親ページの手順に従ってください。コンテンツタイプ名は「Car Brand」になります)。

コンテンツタイプへの追加フィールドの追加

このステップを実行するには2つの方法があります。UI を使用してフィールドを作成し、設定をファイルにエクスポートするか、ファイルを自分で直接書くことができます。私は最初のオプションを使用するのが好きです。必要になるものを何も見逃していないことを確認できるからです。ただし、フィールドはカスタムコンテンツタイプと関連付けるために「foobar_car_brand_」ではなく「field_」で始まる名前でエクスポートされます。このページでは、カスタムモジュールにフィールドを手動でコーディングする方法を説明します。

モジュールでのフィールドの手動コーディング

前述のように、この方法では、フィールドに何をさせたいか、どのように見せたいかについて、もう少し知識と正確な理解が必要です。追加するフィールドごとに、設定情報を含む2つの YAML ファイルが必要です。それらの名前は次のとおりです: field.field.node.car_brand.field_brand_information.yml と field.storage.node.field_brand_information.yml。

カスタムコンテンツタイプの削除

前のページの最後で foobar モジュールを既に有効にしている場合は、今すぐ続行してアンインストールします。

foobar/config/install/field.storage.node.field_brand_information.yml

このファイルは、Drupal にフィールドを作成する必要があることを通知します。

# field.storage.node.field_brand_information.yml
langcode: en
status: true
dependencies:
  module:
    - node
    - text
id: node.field_brand_information
field_name: field_brand_information
entity_type: node
type: text_with_summary
settings: {  }
module: text
locked: false
cardinality: 1
translatable: true
indexes: {  }
persist_with_no_fields: false
custom_storage: false

 

foobar/config/install/field.field.node.car_brand.field_brand_information.yml

このファイルは、Drupal にフィールドをコンテンツタイプにアタッチする必要があることを通知します。

# field.field.node.car_brand.field_brand_information.yml
langcode: en
status: true
dependencies:
  config:
    - field.storage.node.field_brand_information
    - node.type.car_brand
  module:
    - text
id: node.car_brand.field_brand_information
field_name: field_brand_information
entity_type: node
bundle: car_brand
label: 'Brand Information'
description: 'More specific information about the car brand'
required: false
translatable: false
default_value: {  }
default_value_callback: ''
settings:
  display_summary: false
field_type: text_with_summary

foobar/config/install/core.entity_form_display.node.car_brand.default.yml | foobar/config/install/core.entity_view_display.node.car_brand.default.yml

これらのファイルは両方とも、追加するすべての新しいフィールドが含まれるように、前のページから更新する必要があります。複数のフィールドがある場合の様子を見ていただくために、私は「Additional_field_1」と「Additional_field_2」という名前の追加フィールドを2つ追加しました。

注: 追加フィールドを作成しない場合は、次のコードでこれらのフィールドの依存関係と情報を削除してください。

# core.entity_view_display.node.car_brand.default.yml
langcode: en
status: true
dependencies:
  config:
    - field.field.node.car_brand.field_brand_information
    - field.field.node.car_brand.field_additional_field_1
    - field.field.node.car_brand.field_additional_field_2
    - node.type.car_brand
  module:
    - file
    - text
    - user
_core:
  default_config_hash: Nfnv6VMugBKl6EOqi_U0I_LQ1ZQpbNDN3a9GXHWUBz4
id: node.car_brand.default
targetEntityType: node
bundle: car_brand
mode: default
content:
  field_brand_information:
    weight: 101
    label: above
    settings: {  }
    third_party_settings: {  }
    type: text_default
  field_additional_field_1:
    weight: 103
    label: above
    settings:
      link_to_entity: false
    third_party_settings: {  }
    type: string
  field_additional_field_2:
    weight: 102
    label: above
    settings: {  }
    third_party_settings: {  }
    type: file_default
  links:
    weight: 100
hidden: {  }

 

# core.entity_form_display.node.car_brand.default.yml
langcode: en
status: true
dependencies:
  config:
    - field.field.node.car_brand.field_brand_information
    - field.field.node.car_brand.field_additional_field_1
    - field.field.node.car_brand.field_additional_field_2
    - node.type.car_brand
  module:
    - file
    - path
    - text
_core:
  default_config_hash: qZE-qJ04DTTNggVVQdVOPQmpE_I69GQ_LqB32kXivVg
id: node.car_brand.default
targetEntityType: node
bundle: car_brand
mode: default
content:
  created:
    type: datetime_timestamp
    weight: 2
    settings: {  }
    third_party_settings: {  }
  field_brand_information:
    weight: 7
    settings:
      rows: 9
      summary_rows: 3
      placeholder: ''
    third_party_settings: {  }
    type: text_textarea_with_summary
  field_additional_field_1:
    weight: 6
    settings:
      size: 60
      placeholder: ''
    third_party_settings: {  }
    type: string_textfield
  field_additional_field_2:
    weight: 8
    settings:
      progress_indicator: throbber
    third_party_settings: {  }
    type: file_generic
  path:
    type: path
    weight: 5
    settings: {  }
    third_party_settings: {  }
  promote:
    type: boolean_checkbox
    settings:
      display_label: true
    weight: 3
    third_party_settings: {  }
  sticky:
    type: boolean_checkbox
    settings:
      display_label: true
    weight: 4
    third_party_settings: {  }
  title:
    type: string_textfield
    weight: 0
    settings:
      size: 60
      placeholder: ''
    third_party_settings: {  }
  uid:
    type: entity_reference_autocomplete
    weight: 1
    settings:
      match_operator: CONTAINS
      size: 60
      placeholder: ''
    third_party_settings: {  }
hidden: {  }

カスタムコンテンツタイプの有効化

ここで、foobar モジュールを有効にする必要があります。「コンテンツの追加」ページに移動すると、「Car Brand」コンテンツタイプの新しいノードを作成でき、「Brand Information」という名前の新しいフィールドが含まれていることがわかります。

これらのフィールドの UI を使用した追加については、次のページで説明します。