We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 46580
    • 167 Posts
    Hello

    Using Mycomponent I can define property set.
    But I have some difficulties to attach properties to property sets.

    For example I'm trying to attach a property set and its values to a template.

    In the config file I have:
            'templates' => array(
                'b4home' => array(
                    'category' => 'bootstrap4plus',
                    'description' => 'Bootstrap4 homepage sample',
                    'static' => false,
                    'propertySets' => array(
                        'bootstrap4plus1',
                    ),
                ),
            ),
    


    And, in /assets/mycomponents/bootstrap4plus/_build/data/properties/
    A file named "properties.bootstrap4plus1.php" :
    <?php
    
    $properties = array(
        array(
            'name' => 'b4navbar_sticky',
            'desc' => 'b4navbar_sticky_property_desc',
            'type' => 'combo-boolean',
            'options' => '',
            'value' => '1',
            'lexicon' => 'bootstrap4plus:properties',
        ),
         array(
            'name' => 'b4navbar_behaviour',
            'desc' => 'b4navbar_behaviour_property_desc',
            'type' => 'list',
            'options' => array(
                array(
                    'name' => 'Fixed',
                    'value' => 'fixtop',
                    'menu' => '',
                ),
                array(
                    'name' => 'Default',
                    'value' => 'default',
                    'menu' => '',
                ),
            ),
            'value' => 'fixed',
            'lexicon' => 'bootstrap4plus:properties',
        ),
        array(
            'name' => 'b4navbar_container',
            'desc' => 'b4navbar_container_desc',
            'type' => 'list',
            'options' => array(
                array(
                    'name' => 'Normal',
                    'value' => '0',
                    'menu' => '',
                ),
                array(
                    'name' => 'Fluid',
                    'value' => '1',
                    'menu' => '',
                ),
            ),
            'value' => '1',
            'lexicon' => 'bootstrap4plus:properties',
        ),
    );
    
    
    return $properties;
    


    After package installation the property set is created but :

    -- there is no records attached
    -- the property set is not automatically attached to the template

    What's wrong?
      MODX lover
      -
      Développeur MODX / Webdesign / Solutions web
      • 3749
      • 24,544 Posts
      First, did you run ExportObjects after creating the property set and attaching it to the Template? That's where the resolver gets created.

      Is there a property set resolver in _build/resolvers?


      Worst case -- you can merge this code into your generic resolver:

      if (!function_exists('getNameAlias')) {
          function getNameAlias($elementType)
          {
              switch ($elementType) {
                  case 'modTemplate':
                      $nameAlias = 'templatename';
                      break;
                  case 'modCategory':
                      $nameAlias = 'category';
                      break;
                  case 'modResource':
                      $nameAlias = 'pagetitle';
                      break;
                  default:
                      $nameAlias = 'name';
                      break;
              }
              return $nameAlias;
      
          }
      }
      
      if (!function_exists('checkFields')) {
          function checkFields($required, $objectFields) {
              global $modx;
              $fields = explode(',', $required);
              foreach ($fields as $field) {
                  if (!isset($objectFields[$field])) {
                      $modx->log(modX::LOG_LEVEL_ERROR, '[PropertySet Resolver] Missing field: ' . $field);
                      return false;
                  }
              }
              return true;
          }
      }
      if ($object->xpdo) {
          $modx =& $object->xpdo;
          switch ($options[xPDOTransport::PACKAGE_ACTION]) {
              case xPDOTransport::ACTION_INSTALL:
              case xPDOTransport::ACTION_UPGRADE:
              $intersects = array (
                      1 =>  array (  //Change the fields below
                        'element' => 'Template2',
                        'element_class' => 'modTemplate',
                        'property_set' => 'PropertySet2',
                      ),
                  );
      
              if (is_array($intersects)) {
                  foreach ($intersects as $k => $fields) {
                      /* make sure we have all fields */
                      if (!checkFields('element,element_class,property_set', $fields)) {
                          continue;
                      }
                      $elementObj = $modx->getObject($fields['element_class'],
                          array(getNameAlias($fields['element_class']) => $fields['element']));
      
                      $propertySetObj = $modx->getObject('modPropertySet', array('name' => $fields['property_set']));
      
                      if (!$elementObj || !$propertySetObj) {
                          $modx->log(xPDO::LOG_LEVEL_ERROR, 'Could not find Element and/or Property Set ' .
                              $fields['element'] . ' - ' . $fields['property_set']);
                          continue;
                      }
                      $fields['element'] = $elementObj->get('id');
                      $fields['property_set'] = $propertySetObj->get('id');
      
                      $tvt = $modx->getObject('modElementPropertySet', $fields);
                      if (!$tvt) {
                          $tvt = $modx->newObject('modElementPropertySet');
                      }
                      if ($tvt) {
                          foreach($fields as $key => $value) {
                              $tvt->set($key, $value);
                          }
                          if (!$tvt->save()) {
                              $modx->log(xPDO::LOG_LEVEL_ERROR, 'Unknown error creating elementPropertySet intersect for ' .
                                  $fields['element'] . ' - ' . $fields['property_set']);
                          }
      
                      } else {
                          $modx->log(xPDO::LOG_LEVEL_ERROR, 'Unknown error creating elementPropertySet intersect for ' .
                              $fields['element'] . ' - ' . $fields['property_set']);
                      }
                  }
              }
                  break;
      
              case xPDOTransport::ACTION_UNINSTALL:
                  break;
          }
      }
      
      return true;


        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 46580
        • 167 Posts
        Quote from: BobRay at Apr 10, 2018, 05:37 PM
        First, did you run ExportObjects after creating the property set and attaching it to the Template? That's where the resolver gets created.

        Is there a property set resolver in _build/resolvers?



        Yes :
                        0 =>  array (
                          'element' => 'b4home',
                          'element_class' => 'modTemplate',
                          'property_set' => 'bootstrap4plus1',
                        ),
        


        Is
        /assets/mycomponents/bootstrap4plus/_build/data/properties/
        is the right place to put property set definition ?
          MODX lover
          -
          Développeur MODX / Webdesign / Solutions web
          • 3749
          • 24,544 Posts
          If the property set and the template are being created on install, all you need to do is create the modElementPropertySet intersect connecting the property set to the template (as in the code above).

          It should be happening automatically if they're connected in MODX. You might need to manually clear the MODX cache before running Export Objects and then Build.
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting