The 2 years restriction applies only to those with a permanent residence permit. You can’t apply for one until you’ve been a resident for 5 years.
Residence permits have a absence from Portugal restriction. During the valid period of the permit you can be absent from Portugal for 6 months in a row, or 8 months otherwise. So, for a permit with a 2 year valid period, you must stay in Portugal for 16 months (24-8=16) or 18 months (24-6). For a permit with a 3 year valid period, you must stay in Portugal for 28 months (36-8) or 30 months (30-6)
Article 85 - Cancellation of the residence permit
2 - Without prejudice to the application of special provisions, the residence permit may also be canceled when the person concerned, without reasonable reasons, is absent from the country:
a) Being the holder of a temporary residence permit, six consecutive months or eight interpolated months, during the total period of validity of the authorization;
b) Being the holder of a permanent residence permit, 24 consecutive months or, in a period of three years, 30 interpolated months.
(Site is in Portuguese. Chrome browser translates)
[1 ]According to the law, there are only 2 types of residence permits - temporary and permanent. Temporary ones now have valid periods of 2 or 3 years. See Article 74. Item 2 in the Comments section provides more explanation.